SQL学习笔记(一)—— join & union

序言

      MySQL中查询功能是不可缺少的,而且在查询过程中会使用各种join方式得到我们想要的数据,因此join的灵活应用是不可或缺的。当然,所有的连接都是忠实于我们的数据处理目的,SQL各种功能语句的应用就是我们达成目的的工具,系统性虽不强,但积累应用是重中之重


JOIN 各种连接


join的各种连接方式

1.左连接 left (outer) join

关键字会从左表那里返回所有的行,即使在右表中没有匹配的行


left join(1)

select * from table A left join table B on table A.Key=table B.Key

left join(2)

select * from table A left join table B on table A.Key=table B.Key where table B.Key is null

从不订购的客户题目分析

从不订购的客户

题目来源:https://leetcode-cn.com/problems/customers-who-never-order/

解题思路:

为了得到从不订购的客户,说明Customers表中的客户Id没有出现在Orders表的CustomerId中

以Customers作为左表,Orders作为与之连接的表,Orders中的CustomersId对应Customers表中的Id,且Orders中的CustomersId全部都在Customers表的Id中,且个数小于等于Customers表中的Id数。通过左连接得到4个字段,分别对应Customers表中的Id,Name 和Orders表中的Id,CustomersId。没有订购的客户连接之后对应Orders的字段列Id和CustomersId是查询不到的,因此会显示 null,完整的语句如下:

select Name Customers

from Customers c left outer join Orders o

on c.Id=o.CustomerId 

where o.Id is null

2.右连接 right (outer) join

right join 关键字会从右表里返回所有的行,即使在左表中没有对应匹配的行


right join (1)

select * from table A right join table B on table A.Key=table B.Key


right join (2)

select * from table A right join table B on table A.Key=table B.Key where table A.Key is null

基于上面未订购用户的例子,使用右连接方式做题,稍微修改一下顺序即可

select Name Customers                            不变          select Name Customers

from Customers c left outer join Orders o 改成          from Orders o left outer join  Customers c

on c.Id=o.CustomerId                                不变          on c.Id=o.CustomerId 

where o.Id is null                                        不变          where o.Id is null 

            左连接                                                                      右连接                      


3. inner join 内连接(两个集合的交集)


inner join

select * from table A inner join table B on table A.Key=table B.Key

(类似Excel中的VLOOKUP函数)

4.full join

SQL FULL JOIN结合的左,右外连接的结果,连接表将包含的所有记录来自两个表,并使用NULL值作为两侧缺失匹配结果,MySQL中没有此功能,可以用union 代替


full join (1)

select * from table A full join table B where table A.Key=table B.Key

full join (2)

select *  from table A full join table B where table A.Key=table B.Key where table B.Key is null or table A.Key is null

MySQL union all替代方式

select * col_name from table A left join table B on table A.Key=table B.Key

union 

select *  from table A right join table B on table A.Key=table B.Key

ps: union 使用注意事项,之前这块写错了,使用了union all,union all是不去重的,但是采用左连接和右连接两个集合整体交集部分多加了一次,需要去重所以应使用union

5.cross join

CROSE JOIN返回两张表的笛卡尔积,也就是不指定结合规则,让两表中的元素直接两两组合

:table A               table B     

                                             select * from table A cross join table B (cross join 可省略用“,”代替)

        c1                        c2-----> c1            c2

        1                           4            1            4

        2                           5            1            5

        3                           6            1            6

                                                   2            4

                                                   2            5

                                                   2            6

                                                   3            4

                                                   3            5

                                                   3            6



union 

union查询必须满足的条件

1)两个查询返回的数据列数必须相同

2)  两个select语句对应列返回的数据类型必须相同(至少兼容)

union & union all 区别

union上下两张表有重复的记录时,union会对整体结果进行去重                                union all则展示所有结果

你可能感兴趣的:(SQL学习笔记(一)—— join & union)