当前位置:网站首页>Multi table operation - external connection query

Multi table operation - external connection query

2022-07-04 19:41:00 Soup key TJ

Catalog

The left outer join

Right connection

Example operation


  • External connection query is divided into left external connection query and right external connection query
  • Take a table as Lord , Take out all the records in it , Then each one is joined to another table , Whether or not you can match the conditions , In the end, they will keep
  • Can match , Keep it right ; Can't match , The fields of other tables are empty (null), Called external connection
  • The left outer join

  • Query principle
  • Query all the data in the left table , And the left and right tables have the intersection part of the data
  • The query syntax
  • select Name from Table name 1 left [outer] join Table name 2 on Conditions
  • Right connection

  • Query principle
  • Query all the data in the right table , And the left and right tables have the intersection part of the data
  • The query syntax
  • select Name from Table name 1 right [outer] join Table name 2 on Conditions
  • Example operation

  • Please click to view the data preparation of the table
  • The left outer join
  • Query all user information , And the corresponding order information of the user
  • --  The left outer join 
    --  Query all user information , And the corresponding order information of the user 
    SELECT
    		u.*,
    		o.number
    FROM
        user u
    LEFT OUTER JOIN
        orderlist o
    ON
    		o.uid=u.id;
  • Right connection
  • Query all order information , And the user information of the order
  • --  Right connection 
    --  Query all order information , And the user information of the order 
    SELECT
    		o.*,
    		u.name
    FROM
        user u
    RIGHT OUTER JOIN
        orderlist o
    ON
    		o.uid=u.id;
原网站

版权声明
本文为[Soup key TJ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041813129965.html