码迷,mamicode.com
首页 > 数据库 > 详细

MySQL查询语句

时间:2019-05-29 14:42:22      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:聚合   like   with   嵌套   字段排序   函数   忽略   order   ade   

-- 查询全部内容
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查询语句

标签:聚合   like   with   嵌套   字段排序   函数   忽略   order   ade   

原文地址:https://www.cnblogs.com/X-JY/p/10943394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!