# 多表连表查询 join union
# 1、表链的语法
# 1.1 横向连表查询
多表连接查询,实际上是两个表中有一个或者多个字段是有关联关系的,那么在查询时候可以使用连接查询先把2个表关联起来再进行查询。 这种语法遵循下面的基本语法
select * from $table1 left/right/inner join $table2 on $table1.aaa=$table2.bbb and $table1.ccc=$table2.ddd
语句解析:
- $table1 和 $table2代表连表的两个表名
- left/right/inner join代表表链接方式
- on后面$table1.aaa=$table2.bbb and $table1.ccc=$table2.ddd代表表连接符合的条件
# 1.2 查询结果纵向连接
查询结果字段相同,按不同方式查询的多个查询结果连接起来返回 基本语法
select $column1, $column2 from $table1 union select $column1, $column2 from $table2;
语句解析:
- $column1, $column2是需要查询数据的字段名,也可以不是相同的字段名用 $column3 as $column1,$column4 as $column2
- select $column1, $column2 from $table1 是需要对$table1进行查询的完整查询结果1,select $column1, $column2 from $table2 进行查询的完整查询结果2,这2个查询语句可以加入各种where条件或者其它规则,严格来说它是个完整查询字句。
- union join就是连接关键字, 整个sql语句是查询结果1和查询结果2连接起来作为一个完整查询结果对进行返回
- 当所有字段结果值完全相同的时候,有去重效果
# 2、实例验证
我们初始化一些测试数据(表结构在mysql中进行一次简单的数据存储和查询)
truncate table user;
insert into `user`(`user_code`, `user_name`, `age`, `money`) values("no1", "小明", 32, 1.2);
insert into `user`(`user_code`, `user_name`, `age`, `money`) values("no2", "小芳", 25, 5.2);
insert into `user`(`user_code`, `user_name`, `age`, `money`) values("no3", "小东", 32, 7.2);
insert into `user`(`user_code`, `user_name`, `age`, `money`) values("no4", "小宇", 25, 8.2);
insert into `user`(`user_code`, `user_name`, `age`, `money`) values("no5", "小灵", 21, 8.2);
truncate table user_car;
insert into user_car values("no1", "法拉第");
insert into user_car values("no1", "保时捷");
insert into user_car values("no2", "保时捷");
insert into user_car values("no6", "保时捷");
# 2.1:内连接查询 inner join
select * from `user` inner join user_car on `user`.user_code=user_car.user_code;
select * from `user` join user_car on `user`.user_code=user_car.user_code;
上面2个sql的查询结果都为:
id | user_code | user_name | age | money | user_code | car_name |
---|---|---|---|---|---|---|
1 | no1 | 小明 | 32 | 1.20 | no1 | 保时捷 |
1 | no1 | 小明 | 32 | 1.20 | no1 | 法拉第 |
2 | no2 | 小芳 | 25 | 5.20 | no2 | 保时捷 |
** 查询结果符合条件user.user_code=user_car.user_code的数据被保留了 **
# 2.2:左链接查询 left join
select * from `user` left join user_car on `user`.user_code=user_car.user_code;
查询结果为:
id | user_code | user_name | age | money | user_code | car_name |
---|---|---|---|---|---|---|
1 | no1 | 小明 | 32 | 1.20 | no1 | 保时捷 |
1 | no1 | 小明 | 32 | 1.20 | no1 | 法拉第 |
2 | no2 | 小芳 | 25 | 5.20 | no2 | 保时捷 |
3 | no3 | 小东 | 32 | 7.20 | [NULL] | [NULL] |
4 | no4 | 小宇 | 25 | 8.20 | [NULL] | [NULL] |
5 | no5 | 小灵 | 21 | 9.20 | [NULL] | [NULL] |
** 查询结果中左表(user)不符合条件user.user_code=user_car.user_code的数据被保留,同行数据中右表(user_car)的列全是空,而右表的数据不和条件的都被过滤掉了 **
# 2.3:左链接查询 right join
select * from `user` right join user_car on `user`.user_code=user_car.user_code;
查询结果为:
id | user_code | user_name | age | money | user_code | car_name |
---|---|---|---|---|---|---|
1 | no1 | 小明 | 32 | 1.20 | no1 | 保时捷 |
1 | no1 | 小明 | 32 | 1.20 | no1 | 法拉第 |
2 | no2 | 小芳 | 25 | 5.20 | no2 | 保时捷 |
[NULL] | [NULL] | [NULL] | [NULL] | [NULL] | no6 | 保时捷 |
查询结果正好和left join相反,右表(user_car)的数据被保留了,坐标不被保留
# 2.4:查询结果连接 union
# 2.4.1 普通连接
select user_code, user_name as name from `user` union select user_code, car_name as name from user_car
查询结果为:
user_code | name |
---|---|
no1 | 小明 |
no2 | 小芳 |
no3 | 小东 |
no4 | 小宇 |
no5 | 小灵 |
no1 | 法拉第 |
no1 | 保时捷 |
no2 | 保时捷 |
no6 | 保时捷 |
上面的查询结果反映了2个查询字句结果连接起来返回结果
# 2.4.2 有去重的连接
select user_code from `user` union select user_code from user_car
查询结果为:
user_code |
---|
no1 |
no2 |
no3 |
no4 |
no5 |
no6 |
可以看到2个查询结果进行连接,但是查询结果又进行去重
← 查询结果查询 group 子查询 →