• Mysql的join语句


    mysql超强功能之一:join

    # group by 必须放在 order by 和 limit之前,不然会报错
    # 你可以在 SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。
    # JOIN 常用分为如下三类(但不仅仅只有这三类): 
    # INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录;使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样)
    # LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
    # RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

    1. 首先是两张表:

    select * from my_test_copy;  #第一张表

    select * from my_test;  #第二张表

    2. cross join,类似一次join多个表,求笛卡尔积

    select a.id, a.name_people, b.name_adress from my_test_copy as a, my_test as b; #可用
    select a.id, a.name_people, b.name_adress from my_test_copy as a cross join my_test as b; #与上一句结果一致
    
    select * from my_test_copy as a, my_test as b; #可用
    select * from my_test_copy as a cross join my_test as b; #与上一句结果一致

    3.  inner join会连接形成新的列

    select a.id, a.name_people, b.name_adress from my_test_copy as a inner join my_test as b on a.name_adress=b.name_adress; #可用,或者用:(即不用 'as' 也行)
    select a.id, a.name_people, b.name_adress from my_test_copy a inner join my_test b on a.name_adress=b.name_adress; #也可用

    select a.id, a.name_people, a.name_adress from my_test a inner join my_test_copy b on a.name_adress=b.name_adress;   #针对于on的对应列进行笛卡尔积,返回所需要的列

    select * from my_test a inner join my_test_copy b on a.name_adress=b.name_adress;  #相同列会自动加数字表示差异

    4. 左连接 left join,不管怎样,a的值都会返回,b的值中如果不存在则用NULL表示;连接表示连在一起,形成新的列,也算笛卡尔积 <a, b>;左连接LEFT JOIN的含义就是求两个表的交集外加左表剩下的数据。依旧从笛卡尔积的角度讲,就是先从笛卡尔积中挑出ON子句条件成立的记录,然后加上左表中剩余的记录(见结果中最后三条)。

    select a.id, a.name_people, a.name_adress, b.id as b_id, b.name_people as name_people from my_test_copy a left join my_test b on a.name_adress=b.name_adress;

    select * from my_test_copy a left join my_test b on a.name_adress=b.name_adress where b.name_adress is NULL; # 即left join中左边多余的部分

    5. 右连接 right join,不管怎样,b的值都会返回,a的值中如果不存在则用NULL表示;连接表示连在一起,形成新的列,也算笛卡尔积 <a, b>;同理右连接RIGHT JOIN就是求两个表的交集外加右表剩下的数据。再次从笛卡尔积的角度描述,右连接就是从笛卡尔积中挑出ON子句条件成立的记录,然后加上右表中剩余的记录。

    select a.id, a.name_people, a.name_adress, b.id as b_id, b.name_people as name_people from my_test_copy a right join my_test b on a.name_adress=b.name_adress;

    select * from my_test_copy a right join my_test b on a.name_adress=b.name_adress where a.name_adress is NULL; # 即right join中左边多余的部分,此时该表为空。

    6. 使用 using进行连接,using与on的效果相同,但是要两个表的列名相同

    # using 
    select * from my_test_copy right join my_test using(name_adress);

    7. where也可以用于连接

    select * from my_test_copy, my_test where my_test_copy.name_adress=my_test.name_adress;  # 这样也可以做到连接两个表

    8. 附加:外连接,即求两个集合的并集。从笛卡尔积的角度讲就是从笛卡尔积中挑出ON子句条件成立的记录,然后加上左表中剩余的记录,最后加上右表中剩余的记录。另外MySQL不支持OUTER JOIN,但是我们可以对左连接和右连接的结果UNION操作来实现,例如:

    select * from my_test_copy a left join my_test b on a.name_adress=b.name_adress
    union 
    select * from my_test_copy a right join my_test b on a.name_adress=b.name_adress;

    参考:

    https://www.cnblogs.com/beili/p/9140019.html

    https://www.runoob.com/w3cnote/sql-join-image-explain.html

    https://www.runoob.com/mysql/mysql-join.html

  • 相关阅读:
    idou老师教你学Istio 19 : Istio 流量治理功能原理与实战
    面对runc逃逸漏洞,华为云容器为您保驾护航
    idou老师教你学Istio 18 : 如何用istio实现应用的灰度发布
    idou老师教你学Istio 17 : 通过HTTPS进行双向TLS传输
    idou老师教你学Istio 16:如何用 Istio 实现微服务间的访问控制
    idou老师教你学Istio 15:Istio实现双向TLS的迁移
    极简容器化交付 | 部署组件分析
    idou老师教你学Istio 14:如何用K8S对Istio Service进行流量健康检查
    Hibernate5笔记9--Hibernate注解式开发
    Hibernate5笔记8--Hibernate事务相关内容
  • 原文地址:https://www.cnblogs.com/qi-yuan-008/p/12337587.html
Copyright © 2020-2023  润新知