MySQL的联结(Join)以及MySQL求交集和差集
在SQL标准中规划的(Join)联结大致分为下面四种:
1. 内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结。
2. 外联结:分为外左联结和外右联结。
左联结A、B表的意思就是将表A中的全部记录和表B中联结的字段与表A的联结字段符合联结条件的那些记录形成的记录集的联结,这里注意的是最后出来的记录集会包括表A的全部记录。
右联结A、B表的结果和左联结B、A的结果是一样的,也就是说:
Select A.name B.name From A Left Join B On A.id=B.id
和Select A.name B.name From B Right Join A on B.id=A.id执行后的结果是一样的。
3.全联结:将两个表中存在联结关系的字段的所有记录取出形成记录集的联结(这个不需要记忆,只要是查询中提到了的表的字段都会取出,无论是否符合联结条件,因此意义不大)。
4.无联结:不用解释了吧,就是没有使用联结功能呗,也有自联结的说法。
1.USING (column_list):其作用是为了方便书写联结的多对应关系,大部分情况下USING语句可以用ON语句来代替,如下面例子:
a LEFT JOIN b USING (c1,c2,c3),其作用相当于下面语句
a LEFT JOIN b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3
只是用ON来代替会书写比较麻烦而已。
mysql> desc A;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | MUL | NULL | |
| addr | varchar(5) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> desc B;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | MUL | NULL | |
| addr | varchar(5) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from A;
+------+------------+------+------+
| id | name | addr | age |
+------+------------+------+------+
| 1 | kenthy | wh | 25 |
| 2 | jimmy | wh | 26 |
| 3 | kenthyzhan | sz | 26 |
| 4 | jiemzhang | sh | 24 |
+------+------------+------+------+
4 rows in set (0.00 sec)
mysql> select * from B;
+------+------------+------+------+
| id | name | addr | age |
+------+------------+------+------+
| 1 | kenthy | wh | 25 |
| 2 | jimmy | wh | 28 |
| 3 | kenthyzhan | sz | 26 |
+------+------------+------+------+
3 rows in set (0.00 sec)
交集:
mysql> select A.* from A inner join B using(name,addr,age);
+------+------------+------+------+
| id | name | addr | age |
+------+------------+------+------+
| 1 | kenthy | wh | 25 |
| 3 | kenthyzhan | sz | 26 |
+------+------------+------+------+
2 rows in set (0.00 sec)
差集:(在A中出现没有在B中出现的,这里的left join你可以对应的换成right join的)
mysql> select A.* from A left join B using(name,addr,age) where B.name is NULL;
+------+-----------+------+------+
| id | name | addr | age |
+------+-----------+------+------+
| 2 | jimmy | wh | 26 |
| 4 | jiemzhang | sh | 24 |
+------+-----------+------+------+
2 rows in set (0.00 sec)
差集:(在B中出现没有在A中出现的)
mysql> select B.* from B left join A using(name,addr,age) where A.id is NULL;
+------+-------+------+------+
| id | name | addr | age |
+------+-------+------+------+
| 2 | jimmy | wh | 28 |
+------+-------+------+------+
1 row in set (0.00 sec)
部分差集:(A中的数据没有在B中特定部分中存在的)
select A.name,A.addr,A.age from A left join (select * from B where name='kenthy') as C using(name, addr, age) where C.id is NULL;
+------------+------+------+
| name | addr | age |
+------------+------+------+
| jiemzhang | sh | 24 |
| jimmy | wh | 26 |
| kenthyzhan | sz | 26 |
+------------+------+------+
3 rows in set (0.00 sec)
通俗的讲:
A left join B 的连接的记录数与A表的记录数同
A right join B 的连接的记录数与B表的记录数同
A left join B 等价B right join A
阅读(1374) | 评论(0) | 转发(0) |