SQL中join语句详解

您所在的位置:网站首页 今年黄寺有庙会吗 SQL中join语句详解

SQL中join语句详解

2024-07-06 19:49| 来源: 网络整理| 查看: 265

1.inner join(内连接)

只返回匹配的行。 内连接 select * from table_a a inner join table_b b on a.name = b.name

2.left join(左外连接)

返回左表的全部数据,和右表中满足on条件的行,如果左表的行在右表中没有匹配的数据,那么这一行中右表对应的数据为null。 左外连接 select * from table_a a left join table_b b on a.name = b.name

3.right join(右外连接)

返回右表中所有的行,和左表中满足on条件的行,如果右表的行在左表中没有匹配,那么这一行中左表的对应数据为null。 右外连接 select * form table_a a right join table_b b on a.name = b.name

4.left join excluding inner join(左连接-内连接)

就是在left join查询中加入一个where条件(b.name is null),过滤掉A,B表的交集。 left join 会返回左表的全部,右表中匹配的返回,不匹配的null值填充,本查询刚好过过滤掉右表为null的记录 在这里插入图片描述 select * from table_a a left join table_b b on a.name = b.name where b.name is null

5.right join excluding inner join(右连接-内连接)

就是在right join查询中加入一个where条件(a.name is null),过滤掉A,B表的交集。 right join 会返回右表的全部,左表中匹配的返回,不匹配的用null填充,本查询刚好过滤掉左表为null的记录 在这里插入图片描述 select * from table_a a right join table_b b on a.name = b.name where a.name is null

**

注意MySQL不支持,full outer join

**

6.full outer join(外连接)

会返回左表,右表所有的行,对应表中没有数据以null填充。 在这里插入图片描述 由于mysql数据库并不支持full outer join,所以需要使用left join + left join来代替 select * from table_a a left join table_b b on a.name = b.name union select * from table_a a right join table_b b on a.name = b.name

7.full outer join excluding inner join(外连接-内连接)

在这里插入图片描述 select * from table_a a left join table_b b on a.name = b.name where b.name is null union select * form table_a a right join tablb_b b on a.name = b.name where a.name is null



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3