MySQL查询语句
Posted x-jy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL查询语句相关的知识,希望对你有一定的参考价值。
-- 查询全部内容
SELECT * FROM sc;
-- 查询部分列
SELECT zno,sno,sname FROM student;
-- 限制条件查询部分列
SELECT zno,sno,sname,ssex FROM student WHERE ssex = '女';
-- 限制查询条数
SELECT zno,sno,sname,ssex FROM student WHERE ssex = '女' LIMIT 6;
-- 列别名和表别名:可读性增加,简洁性 as可以省略
SELECT zno as 专业代号,sno as 学号,sname 姓名,ssex 性别 FROM student WHERE ssex = '女' LIMIT 6;
-- DISTINCT避免重复数据
SELECT DISTINCT zno FROM student;
-- 限制查询条件
SELECT * FROM sc WHERE grade >= 70 and grade <= 80;
SELECT * FROM sc WHERE grade BETWEEN 70 and 80;
SELECT * FROM sc WHERE grade < 70 or grade > 80;
SELECT * FROM sc WHERE grade NOT BETWEEN 70 and 80;
-- 学c0001的学号和成绩
SELECT sno,grade FROM sc WHERE cno = 'c0001';
-- 学c0001或c0002的学号和成绩
SELECT sno,grade FROM sc WHERE cno = 'c0001' or cno = 'c0002';
SELECT sno,grade FROM sc WHERE cno in( 'c0001','c0002');
-- 学c0001且c0002的学号和成绩(由in引导的嵌套子查询)
SELECT sno,grade FROM sc WHERE cno = 'c0001' and sno in(SELECT sno FROM sc WHERE cno = 'c0002');
-- 查询成绩在集合(65,75,85,95)中的成绩
SELECT sno,grade FROM sc WHERE grade in(65,75,85,95);
-- 查询成绩不在集合(65,75,85,95)中的成绩
SELECT sno,grade FROM sc WHERE grade not in(65,75,85,95);
-- 查询没有分配专业的学生的成绩 NULL比较不能用等号用is 或is not null替代了等号和不等号
SELECT sno,sname FROM student WHERE zno is null;
SELECT sno,sname FROM student WHERE zno is not null;
-- 更新,修改值
UPDATE student set zno = NULL WHERE sno = '1';
-- 查询王晓婷的信息
SELECT * FROM student WHERE sname = '王晓婷';
-- 模糊查找姓王的同学的信息
SELECT * FROM student WHERE sname like '王%';
-- 查询名字中含有
UPDATE student set sname = '王二晓' WHERE sname = '王千津';
-- 查询名字中带晓的 not like %(n个字符) _(一个字符)
SELECT * FROM student WHERE sname like '%晓%';
-- 查询男女学生人数和总人数
-- 聚合函数:count(*);sum(列);AUG(列);Max();Min()
SELECT ssex,COUNT(ssex) FROM student GROUP BY ssex WITH ROLLUP;
-- GROUP BY 待分类的列名 分类汇总(统计)-- ORDER BY 排序
-- 查询各个专业的人数 *不忽略null值,对null也计数
SELECT zno,COUNT(*) FROM student GROUP BY zno;
-- 对空值不计数
SELECT zno,COUNT(zno) FROM student GROUP BY zno;
-- 只显示专业人数超过5的专业
SELECT zno,COUNT(*) FROM student GROUP BY zno HAVING COUNT(zno)>=5;
-- 查询学习2门以上课程的学号和门数
SELECT sno,count(*) FROM sc GROUP BY sno HAVING COUNT(*)>=2;
-- 查询student中按zno字段排序 默认升序ASC 降序DESC
SELECT * FROM student ORDER BY zno DESC,sno DESC;
以上是关于MySQL查询语句的主要内容,如果未能解决你的问题,请参考以下文章
[AndroidStudio]_[初级]_[配置自动完成的代码片段]