mysql中的高级连接查询

 

对于没有学习过数据库的朋友,各种增删改查操作的语法很是头痛,后续多表查询中内外连接更是头懵,多表联查操作,实际就是在基础的查询语句里不同的位置跟子句进行查询,现以mysql为例总结一下:

 

存在学生信息表student:

mysql> select * from student;

+-----+-------+------+----------+------+
| sno | sname | sex | saddress | sage |
+-----+-------+------+----------+------+
| 1 | 李四 | 男 | 上海 | 19 |
| 3 | 王五 | 女 | 湖北武汉 | 18 |
| 4 | 赵六 | 女 | 海南三亚 | 22 |
+-----+-------+------+----------+------+
3 rows in set (0.00 sec))

存在分数表score:

mysql> select * from score;

+----+------+------+-------+
| id | sid | cid | score |
+----+------+------+-------+
| 1 | 3 | 1 | 98 |
| 2 | 4 | 1 | 88 |
| 3 | 1 | 1 | 56 |
| 4 | 4 | 2 | 79 |
+----+------+------+-------+
4 rows in set (0.00 sec)

存在课程表course:

mysql> select * from course;

+----------+------------+
| courseid | coursename |
+----------+------------+
| 1 | mysql |
| 2 | linux |
+----------+------------+
2 rows in set (0.05 sec)


一、where条件后面的子句

等值连接:只返回两个表中连接字段相等的行

举例:查询每个参加了考试的学生信息

mysql> select * from score,student where score.sid=student.sno;
+----+------+------+-------+-----+-------+------+----------+
| id | sid | cid | score | sno | sname | sex | saddress |
+----+------+------+-------+-----+-------+------+----------+
| 1 | 3 | 1 | 98 | 3 | 王五 | 女 | 湖北武汉 |
| 2 | 4 | 1 | 88 | 4 | 赵六 | 女 | 海南三亚 |
+----+------+------+-------+-----+-------+------+----------+
2 rows in set (0.00 sec)

2.非等值连接:做多表连接时使用非’=’号,而使用其它连接运算符的

存在表grade:

mysql> select * from grade;
+----+------+------+------+
| id | low | high | rank |
+----+------+------+------+
| 1 | 80 | 100 | A |
| 2 | 70 | 79 | B |
| 3 | 60 | 69 | C |
| 4 | 0 | 59 | D |
+----+------+------+------+
4 rows in set (0.00 sec)

举例:查询每个分数的等级

mysql> select score,rank from score,grade where score.score between low and high;
+-------+------+
| score | rank |
+-------+------+
| 98 | A |
| 88 | A |
| 79 | B |
+-------+------+
3 rows in set (0.00 sec)

3.自连接:把表虚拟成两个表,自身跟自身进行比较

举例:查询有哪些人比王五大

mysql> select b.sname from student a,student b where b.sage > a.sage and a.sname='王五';
+-------+
| sname |
+-------+
| 李四 |
| 赵六 |
+-------+
2 rows in set (0.00 sec)

4.左连接:外连接中的一种,会显示左表中的所有数据及右表满足条件的数据
举例:查询所有学生的考试情况


mysql> select * from student left join score on student.sno=score.sid;

+-----+-------+------+----------+------+------+------+------+-------+
| sno | sname | sex | saddress | sage | id | sid | cid | score |
+-----+-------+------+----------+------+------+------+------+-------+
| 3 | 王五 | 女 | 湖北武汉 | 18 | 1 | 3 | 1 | 98 |
| 4 | 赵六 | 女 | 海南三亚 | 22 | 2 | 4 | 1 | 88 |
| 4 | 赵六 | 女 | 海南三亚 | 22 | 4 | 4 | 2 | 79 |
| 1 | 李四 | 男 | 上海 | 19 | NULL | NULL | NULL | NULL |
+-----+-------+------+----------+------+------+------+------+-------+
4 rows in set (0.01 sec

5.右连接:外连接中的一表,会显示右表中所有的数据及左表满足条件的数据

举例:查询参数了考试的学生信息

mysql> select * from student right join score on student.sno=score.sid;
+------+-------+------+----------+------+----+------+------+-------+
| sno | sname | sex | saddress | sage | id | sid | cid | score |
+------+-------+------+----------+------+----+------+------+-------+
| 3 | 王五 | 女 | 湖北武汉 | 18 | 1 | 3 | 1 | 98 |
| 4 | 赵六 | 女 | 海南三亚 | 22 | 2 | 4 | 1 | 88 |
| 4 | 赵六 | 女 | 海南三亚 | 22 | 4 | 4 | 2 | 79 |
+------+-------+------+----------+------+----+------+------+-------+
3 rows in set (0.00 sec)

6.子查询:当一个查询是另一个查询的条件时,称之为子查询
举例:查询比王五大的学生

mysql> select sname from student where sage > (select sage from student where sname='王五');
+-------+
| sname |
+-------+
| 李四 |
| 赵六 |
+-------+
2 rows in set (0.00 sec)

二、from后面的子句
派生表:按条件查询结果并派生成临时表,可供查询,但一定话题要带别名
举例:选修了mysql这门课的学生有哪些?

mysql> select * from (select * from score ,course where score.cid=course.courseid and course.coursename='mysql')b ,student a where b.sid = a.sno;
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
| id | sid | cid | score | courseid | coursename | sno | sname | sex | saddress | sage |
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
| 1 | 3 | 1 | 98 | 1 | mysql | 3 | 王五 | 女 | 湖北武汉 | 18 |
| 2 | 4 | 1 | 88 | 1 | mysql | 4 | 赵六 | 女 | 海南三亚 | 22 |
+----+------+------+-------+----------+------------+-----+-------+------+----------+------+
2 rows in set (0.00 sec)

三、select后面的子句
举例:计算mysql这门课中的及格率

mysql> select (select count(*) from score,course where coursename='mysql' and score >=60)/(select count(*) from score,course where coursename='mysql')*100 '及格率';
+---------+
| 及格率 |
+---------+
| 75.0000 |
+---------+
1 row in set (0.00 sec)

posted @   xmx测试员  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示