查询年龄在20到23岁之间的学生信息
select * from student where sage between 20 and 30;
+-----------+-------+------+------+-------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 20 | CS |
+-----------+-------+------+------+-------+
查询计算机系数学系的学生信息
使用关键字in常忽略
select * from student where sdept in('CS','MA');
+-----------+-------+------+------+-------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215123 | 王敏 | 女 | 18 | MA |
+-----------+-------+------+------+-------+
复习:模糊查询规则:
查询所有姓刘的学生
select * from student where sname like '刘%';
+-----------+-------+------+------+-------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215122 | 刘晨 | 女 | 19 | IS |
+-----------+-------+------+------+-------+
查询第二个字为阳的所有学生
select * from student where sname like '_阳%';
Empty set
在sc表中查询成绩为空的信息
select sno,cno from sc where grade is NULL;
多重条件查询即使用and或or进行
查询计算机系年龄在20岁以下的学生
select * from student where sdept='CS' and sage<=20;
+-----------+-------+------+------+-------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----------+-------+------+------+-------+
| 201215121 | 李勇 | 男 | 20 | CS |
+-----------+-------+------+------+-------+
查询选修了三号课程的学号和成绩,按照成绩降序排列
select sno,grade from sc where cno=3 order by grade desc;
+-----------+-------+
| sno | grade |
+-----------+-------+
| 201215121 | 88 |
| 201215122 | 80 |
+-----------+-------+
常用的聚合函数
查询所有学生的人数
select count(*) from student;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
查询选修了课程的学生人数(去重操作)
select count(distinct sno) from sc;
+---------------------+
| count(distinct sno) |
+---------------------+
| 2 |
+---------------------+
求各个课程号相应的人数
select count(sno) cno from sc group by cno;
+-----+
| cno |
+-----+
| 1 |
| 2 |
| 2 |
+-----+
使用分组查询的过程中需要注意合乎逻辑
selecth的后面只能出现分组或者聚合函数
查询选修了1门以上课程的学生的学号(单表查询)
select sno from sc group by sno having count(*) >1;
+-----------+
| sno |
+-----------+
| 201215121 |
| 201215122 |
+-----------+
错误语法实例
select sno from sc where count(*)>1 group by sno;
1111 - Invalid use of group function
查询平均成绩大于70的学生学号和成绩
中间注意用逗号进行列之间的分隔
select Sno AVG(`grade`) from sc group by Sno having AVG(`grade`) >= 85;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`grade`) from sc group by Sno having AVG(`grade`) >= 85' at line 1
mysql>
select sno,AVG(grade) from sc group by sno having AVG(Grade)>70;
+-----------+------------+
| sno | AVG(grade) |
+-----------+------------+
| 201215121 | 88.3333 |
| 201215122 | 85.0000 |
+-----------+------------+
多表查询的分类
字段存在奇异的通过表名来进行改写表名.字段
查询每个学生及选修课的情况(全连接进行显示)
select student.*,sc.* from student,sc where sc.sno = student.sno;
+-----------+-------+------+------+-------+-----------+-----+-------+
| Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade |
+-----------+-------+------+------+-------+-----------+-----+-------+
| 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 1 | 92 |
| 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 2 | 85 |
| 201215121 | 李勇 | 男 | 20 | CS | 201215121 | 3 | 88 |
| 201215122 | 刘晨 | 女 | 19 | IS | 201215122 | 2 | 90 |
| 201215122 | 刘晨 | 女 | 19 | IS | 201215122 | 3 | 80 |
+-----------+-------+------+------+-------+-----------+-----+-------+
查询每个学生及选修课的情况(自然连接进行显示)
select student.*,cno,grade from student,sc where sc.sno = student.sno;
+-----------+-------+------+------+-------+-----+-------+
| Sno | Sname | Ssex | Sage | Sdept | cno | grade |
+-----------+-------+------+------+-------+-----+-------+
| 201215121 | 李勇 | 男 | 20 | CS | 1 | 92 |
| 201215121 | 李勇 | 男 | 20 | CS | 2 | 85 |
| 201215121 | 李勇 | 男 | 20 | CS | 3 | 88 |
| 201215122 | 刘晨 | 女 | 19 | IS | 2 | 90 |
| 201215122 | 刘晨 | 女 | 19 | IS | 3 | 80 |
+-----------+-------+------+------+-------+-----+-------+
5 rows in set (0.05 sec)