MySQL中join的7种方法

Posted 阿凯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL中join的7种方法相关的知识,希望对你有一定的参考价值。

join查询的7中方法

  • 手写顺序:

    SELECT DISTINCT
    	<select_list>
    FROM
    	<left_table> <join_type>
    JOIN <right_table> ON <join_condition>
    WHERE
    	<where_condition>
    GROUP BY
    	<group_by_list>
    HAVING
    	<having_condition>
    ORDER BY
    	<order_by_condition>
    LIMIT <limit_number>
    
  • mysql执行顺序

    FROM <left_table>
    ON <join_condition>
    <join_type> JOIN <right_table>
    WHERE <where_condition>
    GROUP BY <group_by_list>
    HAVING <having_condition>
    DISTINCT <select_list>
    ORDER BY <order_by_condition>
    LIMIT <limit_number>
    

  • join连接方式:

    • 内连接:

 SELECT <select_list> FROM TableA A INNER JOIN TableB B ON A.Key = B.Key
  • 左连接:

 SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key
  • 右连接:

 SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key
  • 只有A

  SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key WHERE B.Key IS NULL
  • 只有B

    SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key WHERE A.Key IS NULL
  • 全连接

    # MySQL没有FULL OUTER语法。
    SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key
    union
    SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key;
  • A,B各自独有:


  # MySQL没有FULL OUTER语法。
  SELECT <select_list> FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key WHERE B.Key IS NULL
  union
  SELECT <select_list> FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key WHERE A.Key IS NULL;

以上是关于MySQL中join的7种方法的主要内容,如果未能解决你的问题,请参考以下文章

MySQL--7种join连接

mysql 7 种 join

MYSQL06_sql99的7种JOIN操作union all

SQL 的 7 种 JOIN

mysql 7种JOIN理论,看完你就懂了

mysql 7种JOIN理论,看完你就懂了