Fork me on GitHub

mysql数据库中表格的联合查询

多表联合查询

1. 环境准备:

 

备注:三个表中至少有一个字段相同(字段表内信息可不完全一致,顺序也不必相同),从而起到三个表格相连的作用

2. 多表联合查询方式:

语法格式:select 标记1.字段名,标记2.字段名,标记1.字段名,标记3.字段名 from 表名1 标记1 ,表名2 标记2,表名3 标记 where 标记1.字段名=标记2.字段名 and 标记1.字段名=标记3.字段名;

MariaDB [dazhu]> select c.id,c.name,c.chengji,j.jingyan,x.xinzi,x.gongsi from chengjibiao c,jianlibiao j,xinzibiao x where c.id=j.id and c.id=x.id;

3.多表联合查询,同时为字段设置as别名:

语法格式:select 标记1.字段名,标记2.字段名 as 别名A,标记1.字段名 as 别名1,标记3.字段名 as 别名c from 表名1 标记1 ,表名2 标记2,表名3 标记3  where 标记1.字段名=标记2.字段名 and 标记1.字段名=标记3.字段名;

MariaDB [dazhu]> select c.id as '编号',c.name,c.chengji,j.jingyan,x.xinzi,x.gongsi as '单位' from chengjibiao c,jianlibiao j,xinzibiao x where c.id=j.id and c.id=x.id;
##这样,id和gongsi字段名就会分别被显示为‘编号’和‘单位’

4.多表联合查询,外连接:

一、左连接;

以左边表格为主,包含所有左边表中的记录甚至是右边表中没有和它匹配的记录。因此可以用于查询未产生第二表格数据的第一表格内的元素信息!

基本格式:select 字段/* from 左表 left 右表 on 左表字段=右表字段;

二.右连接

以右边表格为主,包含所有右边表中的记录.

基本格式:select 字段/* from 左表 right 右表 on 左表字段=右表字段;

实例:

以左边为主
select * from xueyuanbiao left join xinzibiao on xueyuanbiao.id = xinzibiao.id;
##出了可以查看学员就业情况之外,还可以查询出哪个学员没找到工作
MariaDB [dazhu]> select c.id as "编号",c.name,c.chengji,x.name,x.xinzi as "biao c left join xinzibiao x on c.id=x.id;
+--------+--------+---------+--------+--------+
| 编号   | name   | chengji | name   | 薪资   |
+--------+--------+---------+--------+--------+
|      1 | 大傻   |      90 | 大傻   |  18000 |
|      2 | 小二   |      95 | 小二   |  16000 |
|      4 | 六六   |      86 | 六六   |  16000 |
|      5 | 赵五   |     100 | 赵五   |  17000 |
|      6 | 宇宇   |      73 | NULL   |   NULL |
|      3 | 明明   |      80 | NULL   |   NULL |可以发现宇宇和明明可能还未找到工作;
+--------+--------+---------+--------+--------+
6 rows in set (0.00 sec)
以右边为主
MariaDB [dazhu]> select c.id as "编号",c.name,c.chengji,x.name,x.xinzi as "biao c right join xinzibiao x on c.id=x.id;
+--------+--------+---------+--------+--------+
| 编号   | name   | chengji | name   | 薪资   |
+--------+--------+---------+--------+--------+
|      1 | 大傻   |      90 | 大傻   |  18000 |
|      2 | 小二   |      95 | 小二   |  16000 |
|      4 | 六六   |      86 | 六六   |  16000 |
|      5 | 赵五   |     100 | 赵五   |  17000 |只看找到工作的学员的曾经成绩;
+--------+--------+---------+--------+--------+
4 rows in set (0.00 sec)

三.子查询

有时候,当我们查询的时候,需要的条件是另外一个select语句的结果,这时就需要使用子查询。
用于子查询的关键字包括in、not in、=、!=、exists、not exists等

基本语法 :select 字段 from 表名 where 字段 in(条件(可以是字段元素也可以是select语句从其他表提取内容!))

实例:

MariaDB [dazhu]> select * from chengjibiao where id in(select id from xinzi

##先提取xinzibiao中的ID内容让后作为提取成绩表内容的条件id的参数!(可以理解为仅仅在成绩表一个表中偷取内容)
+------+--------+---------+
| id   | name   | chengji |
+------+--------+---------+
|    1 | 大傻   |      90 |
|    2 | 小二   |      95 |
|    4 | 六六   |      86 |
|    5 | 赵五   |     100 |
+------+--------+---------+
4 rows in set (0.00 sec)
MariaDB [dazhu]> select * from chengjibiao where id not in(select id from xinzibiao);
+------+--------+---------+
| id   | name   | chengji |
+------+--------+---------+
|    6 | 宇宇   |      73 |
|    3 | 明明   |      80 |
+------+--------+---------+
2 rows in set (0.00 sec)
......同理,select * from chengjibiao where id = 1;

四、记录联合;

使用 union union all 关键字,将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示。

两者主要区别在于:union去重;union all 不去重,全部显示

实例:

MariaDB [dazhu]> select chengji from chengjibiao union all select xinzi from xinzibiao;
+---------+
| chengji |
+---------+
|      90 |
|      95 |
|      86 |
|     100 |
|      73 |
|      80 |
|   18000 |
|   16000 |
|   16000 |
|   17000 |
+---------+
10 rows in set (0.00 sec)

MariaDB [dazhu]> select chengji from chengjibiao union select xinzi from xinzibiao;
+---------+
| chengji |
+---------+
|      90 |
|      95 |
|      86 |
|     100 |
|      73 |
|      80 |
|   18000 |
|   16000 |
|   17000 |
+---------+
9 rows in set (0.00 sec)

 

posted @ 2020-09-23 11:27  子墨·咖啡  阅读(668)  评论(0编辑  收藏  举报