[MySQL]join的细节
以下示例用到的表:
user | |
id | uid |
3000 | t_001 |
3001 | t_002 |
3002 | t_003 |
issue | |
id | uid |
1 | t_002 |
2 | t_003 |
3 | t_004 |
left join,左表返回所有记录,右表只返回跟左表有关联的记录,当右表有N条记录跟左表的某一条记录A关联,那么查询结果会出现N条A记录(相应关联右表的N条记录)
例:
SELECT `user`.`id` AS `user_id`, `issue`.`id` AS `issue_id` FROM `user` LEFT JOIN `issue` ON `user`.`uid` = `issue`.`uid`;
result | |
user_id | issue_id |
3000 | (null) |
3001 | 1 |
3002 | 2 |
right join,右表返回所有记录,左表只返回跟右表有关联的记录,当左表有N条记录跟右表的某一条记录A关联,那么查询结果会出现N条A记录(相应关联左表的N条记录)
例:
SELECT `user`.`id` AS `user_id`, `issue`.`id` AS `issue_id` FROM `user` RIGHT JOIN `issue` ON `user`.`uid` = `issue`.`uid`;
result | |
user_id | issue_id |
3001 | 1 |
3002 | 2 |
(null) | 3 |
join(inner join),返回两表完全关联一遍的记录,假设左表有3条,右表有4条,则返回的是3*4=12条记录,如果存在ON条件则返回符合ON条件的记录才返回
full join,返回两表完全关联一遍的记录,假设左表有3条,右表有4条,则返回的是3*4=12条记录,不支持ON条件