SQL语句高级(二)

一、多表查询步骤
mysql> select cno,cname from course;
+--------+----------+
| cno    | cname    |
+--------+----------+
| 3-105  | csdaolun |
| 3-245  | os       |
| 6-1166 | math     |
+--------+----------+
3 rows in set (0.06 sec)

mysql> select cno,sno,degree from score;
+--------+-----+--------+
| cno    | sno | degree |
+--------+-----+--------+
| 3-105  | 100 |    100 |
| 3-245  | 100 |     55 |
| 6-1166 | 101 |     59 |
| 3-245  | 102 |    100 |
| 3-105  | 103 |     99 |
| 3-245  | 103 |     89 |
| 6-1166 | 103 |     59 |
| 3-105  | 104 |     59 |
| 3-245  | 104 |     98 |
| 6-1166 | 104 |     89 |
| 3-245  | 105 |     89 |
| 6-1166 | 105 |     89 |
+--------+-----+--------+
12 rows in set (0.00 sec)

mysql> select cname,sno,degree from course,score where course.cno = score.cno;
+----------+-----+--------+
| cname    | sno | degree |
+----------+-----+--------+
| csdaolun | 100 |    100 |
| csdaolun | 103 |     99 |
| csdaolun | 104 |     59 |
| os       | 100 |     55 |
| os       | 102 |    100 |
| os       | 103 |     89 |
| os       | 104 |     98 |
| os       | 105 |     89 |
| math     | 101 |     59 |
| math     | 103 |     59 |
| math     | 104 |     89 |
| math     | 105 |     89 |
+----------+-----+--------+

二、三表关联查询
mysql> select sname,cname,degree from student,course,score
    -> where student.sno = score.sno 
    -> and course.cno=score.cno;
+--------+----------+--------+
| sname  | cname    | degree |
+--------+----------+--------+
| Java   | csdaolun |    100 |
| C#     | csdaolun |     99 |
| Python | csdaolun |     59 |
| Java   | os       |     55 |
| C++    | os       |    100 |
| C#     | os       |     89 |
| Python | os       |     98 |
| JS     | os       |     89 |
| C      | math     |     59 |
| C#     | math     |     59 |
| Python | math     |     89 |
| JS     | math     |     89 |
+--------+----------+--------+
查看更多:
mysql> select sname,cname,degree,student.sno,course.cno from course,score,student 
	-> where course.cno = score.cno
    -> and student.sno = score.sno;
+--------+----------+--------+-----+--------+
| sname  | cname    | degree | sno | cno    |
+--------+----------+--------+-----+--------+
| Java   | csdaolun |    100 | 100 | 3-105  |
| C#     | csdaolun |     99 | 103 | 3-105  |
| Python | csdaolun |     59 | 104 | 3-105  |
| Java   | os       |     55 | 100 | 3-245  |
| C++    | os       |    100 | 102 | 3-245  |
| C#     | os       |     89 | 103 | 3-245  |
| Python | os       |     98 | 104 | 3-245  |
| JS     | os       |     89 | 105 | 3-245  |
| C      | math     |     59 | 101 | 6-1166 |
| C#     | math     |     59 | 103 | 6-1166 |
| Python | math     |     89 | 104 | 6-1166 |
| JS     | math     |     89 | 105 | 6-1166 |
+--------+----------+--------+-----+--------+
用别名:
mysql> select sname,cname,degree,student.sno as stu_sno,course.cno as cou_cno 
    -> from course,score,student 
    -> where course.cno = score.cno and student.sno = score.sno;
+--------+----------+--------+---------+---------+
| sname  | cname    | degree | stu_sno | cou_cno |
+--------+----------+--------+---------+---------+
| Java   | csdaolun |    100 | 100     | 3-105   |
| C#     | csdaolun |     99 | 103     | 3-105   |
| Python | csdaolun |     59 | 104     | 3-105   |
| Java   | os       |     55 | 100     | 3-245   |
| C++    | os       |    100 | 102     | 3-245   |
| C#     | os       |     89 | 103     | 3-245   |
| Python | os       |     98 | 104     | 3-245   |
| JS     | os       |     89 | 105     | 3-245   |
| C      | math     |     59 | 101     | 6-1166  |
| C#     | math     |     59 | 103     | 6-1166  |
| Python | math     |     89 | 104     | 6-1166  |
| JS     | math     |     89 | 105     | 6-1166  |
+--------+----------+--------+---------+---------+

验证:
mysql> select sname,cname,degree,student.sno as stu_sno,score.sno,
	->course.cno as cou_cno,score.cno 
	->from course,score,student 
	->where course.cno = score.cno and student.sno = score.sno;
+--------+----------+--------+---------+-----+---------+--------+
| sname  | cname    | degree | stu_sno | sno | cou_cno | cno    |
+--------+----------+--------+---------+-----+---------+--------+
| Java   | csdaolun |    100 | 100     | 100 | 3-105   | 3-105  |
| C#     | csdaolun |     99 | 103     | 103 | 3-105   | 3-105  |
| Python | csdaolun |     59 | 104     | 104 | 3-105   | 3-105  |
| Java   | os       |     55 | 100     | 100 | 3-245   | 3-245  |
| C++    | os       |    100 | 102     | 102 | 3-245   | 3-245  |
| C#     | os       |     89 | 103     | 103 | 3-245   | 3-245  |
| Python | os       |     98 | 104     | 104 | 3-245   | 3-245  |
| JS     | os       |     89 | 105     | 105 | 3-245   | 3-245  |
| C      | math     |     59 | 101     | 101 | 6-1166  | 6-1166 |
| C#     | math     |     59 | 103     | 103 | 6-1166  | 6-1166 |
| Python | math     |     89 | 104     | 104 | 6-1166  | 6-1166 |
| JS     | math     |     89 | 105     | 105 | 6-1166  | 6-1166 |
+--------+----------+--------+---------+-----+---------+--------+

三、子查询+分组+求平均分(查询95033班学生每门课的平均分)
mysql> select * from student where class = '95033';
+-----+--------+---------------------+-------+-------+
| sno | sname  | sbirthday           | class | ssex  |
+-----+--------+---------------------+-------+-------+
| 100 | Java   | 1977-09-01 00:00:00 | 95033 | man   |
| 104 | Python | 1977-10-11 00:00:00 | 95033 | man   |
| 105 | JS     | 1974-11-11 00:00:00 | 95033 | woman |
+-----+--------+---------------------+-------+-------+
3 rows in set (0.00 sec)

mysql> select sno from student where class = '95033';
+-----+
| sno |
+-----+
| 100 |
| 104 |
| 105 |
+-----+
3 rows in set (0.00 sec)

mysql> select * from score 
 	-> where sno in (select sno from student where class='95033');
+-----+--------+--------+
| sno | cno    | degree |
+-----+--------+--------+
| 100 | 3-105  |    100 |
| 100 | 3-245  |     55 |
| 104 | 3-105  |     59 |
| 104 | 3-245  |     98 |
| 104 | 6-1166 |     89 |
| 105 | 3-245  |     89 |
| 105 | 6-1166 |     89 |
+-----+--------+--------+
7 rows in set (0.07 sec)

mysql> select cno,avg(degree) from score 
	-> where sno in (select sno from student where class='95033')
    -> group by cno;
+--------+-------------+
| cno    | avg(degree) |
+--------+-------------+
| 3-105  |     79.5000 |
| 3-245  |     80.6667 |
| 6-1166 |     89.0000 |
+--------+-------------+
3 rows in set (0.47 sec)




猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87211267