mysql LEFT JOIN多表連接查詢

來源:互聯網
上載者:User

具體操作

MySQL支援Select和某些Update和Delete情況下的Join文法,具體文法上的細節有:
 table_references:
    table_reference [, table_reference] …
 table_reference:
    table_factor
  | join_table
 table_factor:
    tbl_name [[AS] alias]
        [{USE|IGNORE|FORCE} INDEX (key_list)]
  | ( table_references )
  | { OJ table_reference LEFT OUTER JOIN table_reference
        ON conditional_expr }
 join_table:
    table_reference [INNER | CROSS] JOIN table_factor [join_condition]
  | table_reference STRAIGHT_JOIN table_factor
  | table_reference STRAIGHT_JOIN table_factor ON condition
  | table_reference LEFT [OUTER] JOIN table_reference join_condition
  | table_reference NATURAL [LEFT [OUTER]] JOIN table_factor
  | table_reference RIGHT [OUTER] JOIN table_reference join_condition
  | table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor
 join_condition:
    ON conditional_expr | USING (column_list)

除了常用的兩個表串連之外,SQL(MySQL) JOIN 文法還支援多表串連。多表串連基本文法如下:

... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON condition INNER|LEFT|RIGHT JOIN table3 ON condition ...

JOIN 多表串連實現了從多個表中擷取相關資料,下面是三個未經處理資料表:

article 文章表:
aid title content uid tid
1 文章1 文章1本文內容... 1 1
2 文章2 文章2本文內容... 1 2
3 文章3 文章3本文內容... 2 1
5 文章5 文章5本文內容... 4 1
user 使用者表:
uid username email
1 admin admin@5idev.com
2 小明 xiao@163.com
3 Jack jack@gmail.com
type 文章類型表:
tid typename
1 普通文章
2 精華文章
3 草稿

 代碼如下 複製代碼

SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.