详解Mysql两表 join 查询方式
时间:2022-12-31
浏览:107
作者:佚名
怎么入门数据库编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《详解Mysql两表 join 查询方式》,涉及到查询、mysqljoin,有需要的可以收藏一下
一、SQL基本语法格式
SELECT DISTINCT < select_list > FROM < left_table > < join_type > JOIN < right_table > ONWHERE < where_condition > GROUP BY < group_by_list > HAVING < having_condition > ORDER BY < order_by_condition > LIMIT < limit_number >
二、3种join方式
1. left join(左连接)
A left join B 得到A表的所有字段,如果没有匹配到连接条件则用null填充
select A.*,B.* from A left join B on A.id = B.id;

2. right join(右连接)
A right join B 得到B表所有的字段
select A.*,B.* from A right join B on A.id=B.id;

3. inner join(内连接)
A inner join B得到(A和B的交集)
select A.*,B.* from A inner join B on A.id=B.id;

4. 在理解上面的三种join下,查询(A - A∩B)
select A.*,B.* from A left join B on A.id=B.id where B.id is null;

5. 查询 ( B - A∩B )
select A.*,B.* from A right join B on A.id=B.id where A.id is null;

6. 查询(A∪B - A∩B)
利用union去重将上面的第四、第五种两条sql中间用union连接即可完成;即先完成一小部分的,然后将两个拼起来的思想。
select A.*,B.* from A left join B on A.id=B.id where B.id is null union select A.*,B.* from A right join B on A.id=B.id where A.id is null;

7. 查询 AUB
MySQL中求并集可以使用union关键字进行处理(自动去重)
select A.*,B.* from A left join B on A.id=B.id UNION select A.*,B.* from A right join B on A.id=B.id;

好了,本文到此结束,带大家了解了《详解Mysql两表 join 查询方式》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
站内热门
最新文章
12.02
12.02
12.02
12.02
热门专题
