# 右连接 select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui rightjoin mail_info mi on ui.user_id = mi.user_id;
查询结果:
左连接不包含内连接——left
join excluding inner join
这个查询是只查询左边表有的数据,共同有的也不查出来
韦恩图表示如下:
1 2 3 4 5
# 左连接不包含内连接 select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui leftjoin mail_info mi on ui.user_id = mi.user_id where mi.user_id isnull;
# 右连接不包含内连接 select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui rightjoin mail_info mi on ui.user_id = mi.user_id where ui.user_id isnull;
查询结果:
全连接(全外连接)——full
join(full outer join)
查询出左表和右表所有数据,但是去除两表的重复数据 韦恩图表示如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 全连接 # mysql 不支持全连接 # select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id # from user_info as ui # fulljoin mail_info mi on ui.user_id = mi.user_id; # 用以下方式实现,全链接 = 左连接 union 右连接 select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui leftjoin mail_info mi on ui.user_id = mi.user_id union distinct select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui rightjoin mail_info mi on ui.user_id = mi.user_id;
查询结果:
全连接不包含内连接——full
outer join excluding inner join
意思就是查询左右表各自拥有的那部分数据 韦恩图表示如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 全连接不包含内连接 # select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id # from user_info as ui # fulljoin mail_info mi on ui.user_id = mi.user_id # where ui.user_id isnull # or mi.user_id isnull; # 用以下方式实现,全连接不包含内连接 = 左连接不包含内连接 unionall 右连接不包含内连接 select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui leftjoin mail_info mi on ui.user_id = mi.user_id where mi.user_id isnull unionall select ui.user_id, ui.name, ui.lv, mi.mail_id, mi.title, mi.user_id from user_info as ui rightjoin mail_info mi on ui.user_id = mi.user_id where ui.user_id isnull;