多表查询分为:合并结果集、连接查询、子查询
合并结果集:将两个select的结果显示到一起
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!要求表之间的列数,数据类型必须一致!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
select * from T1 union select * from T2; 合并时去重结果
select * from T1 union all select * from T2; 合并时不去重结果
连接查询:一下子查询两张表中的数据
笛卡尔集:假设表A中有m组数据,表B中有n组数据,那么同时查询表A、B能得到m*n组数据。
select * from A,B;
去笛卡尔集:假如两张表之间通过外键关联,那么得到笛卡尔集的结果中很多是无用的,根据关联的外键可以去重结果。
create table A(
a_id bigint primary key auto_increment,
a_name char(10));
create table B(
b_id bigint primary key auto_increment,
b_name char(10),
a_id bigint,
constraint b_to_a foreign key(a_id) references A(a_id));
select * from A,B where A.a_id = B.a_id;
连接方式:内连接、外连接、自然连接
内连接:
等值连接:查询条件中有等号,内连接中的等值连接的结果和去笛卡尔集的结果一样!!!
select * from a inner join b on a.a_id = b.a_id;
左外连接:把左边表的数据全部查出,右边表只显示符合条件的数据;
mysql> select * from a;
+------+--------+---------+
| a_id | a_name | commons |
+------+--------+---------+
| 1001 | 1001A | hello |
| 1002 | 1002A | hello |
+------+--------+---------+
2 rows in set (0.00 sec)
mysql> select * from b;
+------+--------+------+---------+
| b_id | b_name | a_id | commons |
+------+--------+------+---------+
| 2001 | 2001B | 1001 | world |
| 2002 | 2002B | 1002 | world |
+------+--------+------+---------+
2 rows in set (0.00 sec)
mysql> insert into a values(1003,'1003A','new_A');
Query OK, 1 row affected (0.26 sec)
mysql> select * from a left join b on a.a_id = b.a_id;
+------+--------+---------+------+--------+------+---------+
| a_id | a_name | commons | b_id | b_name | a_id | commons |
+------+--------+---------+------+--------+------+---------+
| 1001 | 1001A | hello | 2001 | 2001B | 1001 | world |
| 1002 | 1002A | hello | 2002 | 2002B | 1002 | world |
| 1003 | 1003A | new_A | NULL | NULL | NULL | NULL |
+------+--------+---------+------+--------+------+---------+
3 rows in set (0.00 sec)
右外连接:
mysql> select * from a;
+------+--------+---------+
| a_id | a_name | commons |
+------+--------+---------+
| 1001 | 1001A | hello |
| 1002 | 1002A | hello |
| 1003 | 1003A | new_A |
+------+--------+---------+
3 rows in set (0.00 sec)
mysql> select * from b;
+------+--------+------+---------+
| b_id | b_name | a_id | commons |
+------+--------+------+---------+
| 2001 | 2001B | 1001 | world |
| 2002 | 2002B | 1002 | world |
| 2003 | 2003B | 1001 | new_B |
+------+--------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from a right join b on a.a_id = b.a_id;
+------+--------+---------+------+--------+------+---------+
| a_id | a_name | commons | b_id | b_name | a_id | commons |
+------+--------+---------+------+--------+------+---------+
| 1001 | 1001A | hello | 2001 | 2001B | 1001 | world |
| 1002 | 1002A | hello | 2002 | 2002B | 1002 | world |
| 1001 | 1001A | hello | 2003 | 2003B | 1001 | new_B |
+------+--------+---------+------+--------+------+---------+
3 rows in set (0.00 sec)
mysql>
介绍两种等值连接查询3张表的例子:
select 列名... from a,b,c where a.(xxx) = b.(xxx) and b.(xxx) = c.(xxx);
select 列名... from a
inner join b on a.(xxx) = b.(xxx)
inner join c on c.(xxx) = b.(xxx)
自然连接:连接查询后的结果会产生无用的笛卡尔集,但是会自动找到连接关系去重无用数据。
使用要求为:两张表的列名和数据类型完全一致。