1、查询指定列
select sno,sname from student;
AI 代码解读

2、查询全部列
select * from sc;
AI 代码解读

3、查询经过计算的值
select sname,2018-sage from student; //目标列是表达式
select sname,'Year of birth:',2018-sage,lower(sdept) from student; //目标表达式可以是字符串常量、函数等
select sname NAME,'Year of birth:' BIRTH, 2018-sage BIRTHDAY,lower(sdept) DEPARTMENT from student; //别名
AI 代码解读
select 子句的目标列表达式不仅可以是属性列,还可以是表达式

目标列 不仅可以是算术表达式,还可以是字符串常量、函数等。

还可以通过指定别名来改变查询结果的列标

4、消除重复的行
select distinct sno from sc; //distinct 表示去重
select [all] sno from sc; //如果没有指定distinct,则默认是all
AI 代码解读

条件查询
5、比较大小
用于进行比较的运算符一般包括 =,>,<,>=,<=,!=,!>,!<。最后两个分别是不大于,不小于。
//查询计算机科学系全体学生的名单
select sname from student where sdept='CS';
AI 代码解读

//查询所有年龄在20岁以下的学生姓名及其年龄
select sname,sage from student where sage<20;
AI 代码解读

//查询考试成绩不及格的学生的学号
select distinct sno from sc where grade<60;
AI 代码解读

6、确定范围
谓词 between…and… 和 not between…and… 可以用来查找属性值在(或不在)指定范围内的无组,其中between后是范围的下限(即低值),and 后是范围的上限(即高值)。
//查询年龄在20~30岁(包括20岁和30岁)之间的学生的姓名、系别和年龄。
select sname,sdept,sage from student where sage between 20 and 30;
AI 代码解读

//查询年龄不在20~30岁之间的学生姓名、系别和年龄。
select sname,sdept,sage from student where sage not between 20 and 30;
AI 代码解读

7、确定集合
谓词 in 和 not in 可以用来查找属性值属于或不属于指定集合的元组
//查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别。
select sname,ssex from student where sdept in('CS','MA','IS');
AI 代码解读

//查询既不是计算机科学系(CS)、数学系(MA),也不是信息系(IS)学生的姓名和性别。
select sname,ssex from student where sdept not in('CS','MA','IS');
AI 代码解读

8、字符匹配
谓词 like 可以用来进行字符串的匹配。%(百分号)代表任意长度(长度可以为0)的字符串。_(下划线)代表任意单个字符。
//查询学号为201215121的学生的详细情况
select * from student where sno like '201215121';
//上式可以等价为
select * from student where sno='201215121';
AI 代码解读
如果 like 后面的匹配串不含通配符,则可以用 = (等于)运算符取代 like 谓词,用 != 或 <>(不等于)运算符取代not like 谓词。

//查询所有姓刘的学生的姓名、学号和性别
select sname,sno,ssex from student where sname like '刘%'; //%代表任意长度
AI 代码解读

//查询姓“欧阳”且全名为三个汉字的学生的姓名。
select sname,sno from student where sname like '欧阳_';
// _ 一般用于查询明确长度的字符匹配
AI 代码解读

//查询名字中第二个字为“阳”的学生的姓名和学号
select sname,sno from student where sname like '_阳%';
AI 代码解读

//查询所有不姓刘的学生的姓名、学号和性别
select sname,sno,ssex from student where sname not like '刘%';
AI 代码解读

//查询DB_Design课程的课程号和学分
select cno,ccredit from course where cname like 'DB\\_Design';
AI 代码解读
如果条件里面包含一些特殊的字符,那么就要进行转义了,在mysql里面是在特殊字符前面加 \(反斜杠)进行转义。

//查询以“DB_开头,且倒数第三个字符为 i 的课程的详细情况”
select * from course where cname like 'DB\_%i__';
AI 代码解读

9、涉及空值的查询
//查询缺少成绩的学生的学号和相应的课程号
select sno,cno from sc where grade is null;
//注意:这里的 “is” 不能用等号(=)来代替
AI 代码解读

//查询所有有成绩的学生学号和课程号
select sno,cno
-> from sc
-> where grade is not null;
AI 代码解读

10、多重条件查询
逻辑运算符 and 和 or 可用业连接多个查询条件。and 的优先级高于 or , 但是可以用括号改变优先级。
//查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别
mysql> select sname, ssex from student
-> where sdept='CS' OR sdept='MA' or sdept='IS';
// in 谓词实际上是多个or运算符的缩写
AI 代码解读

//查询计算机科学第年龄在20岁以下的学生姓名
mysql> select sname
-> from student
-> where sdept='CS' and sage<20;
AI 代码解读

10、order by子句
用order by 子句对查询结果一个或多个属性列的升序(asc)或降序(desc)排列,默认值为升序。
//查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。
mysql> select sno,grade
-> from sc
-> where cno='3'
-> order by grade desc;
AI 代码解读

查询全体学生情况,查询结果按所在系的系号升序排列,同一系的学生按年龄降序排列
mysql> select *
-> from student
-> order by sdept,sage desc;
AI 代码解读

11、聚集函数
//查询学生总人数
mysql> select count(*)
-> from student;
AI 代码解读

//查询选修了课程的学生人数
mysql> select count(distinct sno)
-> from sc;
AI 代码解读

//计算选修1号课程的学生平均成绩
mysql> select avg(grade)
-> from sc
-> where cno='1';
AI 代码解读

//查询选修1号课程的学生最高分
mysql> select max(grade)
-> from sc
-> where cno='1';
AI 代码解读

//查询学生201215012选修课程的总学分数
mysql> select sum(ccredit)
-> from sc,course
-> where sno='201215012' and sc.cno=course.cno;
AI 代码解读

当聚集函数遇到空值时,除 count( ) 外,都跳过空值而只处理非空值。count() 是对元组进行计数,某个元组的一个部分列取空值不影响count的统计结果
注意: where 子句中是不能用聚合函数作为条件表达式的。聚集函数只能用于select 子句和 group by 中的 having 子句。
12、group by 子句
group by 子句将查询结果按某一列或多列的值分组,值相等的为一组。分组后聚集函数将作用于每一个组,即每一组都有一个函数值。
//求各个课程号及相应的选课人数
mysql> select cno,count(sno)
-> from sc
-> group by cno;
AI 代码解读

如果分组后还要求按一定的条件对这些级进行筛选,最终只输出满足指定条件的组,则可以使用having短语指定筛选条件。
//查询选修了三门以上课程的学生学号
mysql> select sno
-> from sc
-> group by sno
-> having count(*)>3;
AI 代码解读

where 子句与 having 短语的区别在于作用对象不同。where 子句作用于基本表或视图,从中选择满足条件的元组。having 短语作用于组,从中选择满足条件的组。
//查询平均成绩大于等于90分的学生学号和平均成绩
mysql> select sno,avg(grade)
-> from sc
-> group by sno
-> having avg(grade)>90;
AI 代码解读
