数据库复试—SQL查询举例

数据库复试—SQL查询举例(续)

单表查询实例

查询年龄在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    |
+-----------+-------+------+------+-------+


模糊查询实例

复习:模糊查询规则:

  • %表示任意长度的字符串
  • _表示长度为1的字符
  • escape :对关键字进行忽略

查询所有姓刘的学生

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    |
+-----------+-------+------+------+-------+

orderby语句与聚合函数

  • 查询中涉及到排序操作

查询选修了三号课程的学号和成绩,按照成绩降序排列

select sno,grade from sc where cno=3 order by grade desc;
+-----------+-------+
| sno       | grade |
+-----------+-------+
| 201215121 |    88 |
| 201215122 |    80 |
+-----------+-------+

常用的聚合函数

  • COUNT
  • SUM
  • AVG
  • MAX
  • MIN

查询所有学生的人数

select count(*) from student;
+----------+
| count(*) |
+----------+
|        4 |
+----------+

查询选修了课程的学生人数(去重操作)

 select count(distinct sno) from sc;
+---------------------+
| count(distinct sno) |
+---------------------+
|                   2 |
+---------------------+

groupby分组查询语句

求各个课程号相应的人数

select count(sno) cno from sc group by cno;
+-----+
| cno |
+-----+
|   1 |
|   2 |
|   2 |
+-----+

使用分组查询的过程中需要注意合乎逻辑

having子句查询

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)

你可能感兴趣的:(MySQL,数据库,sql,服务器)