2014-01-17 43 views
0
 
mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
    -> FROM tutorials_tbl a, tcount_tbl b 
    -> WHERE a.tutorial_author = b.tutorial_author; 
+-------------+-----------------+----------------+ 
| tutorial_id | tutorial_author | tutorial_count | 
+-------------+-----------------+----------------+ 
|   1 | John Poul  |    1 | 
|   3 | Sanjay   |    1 | 
+-------------+-----------------+----------------+ 
2 rows in set (0.01 sec) 
mysql>

這是MySQL連接查詢教程http://www.tutorialspoint.com/mysql/mysql-using-joins.htm。但我需要加入許多表格。在那種情況下.. WHERE聲明如何看起來像?請幫助...多個連接表在MySQL

+0

不要在where子句中使用舊的過期隱式連接。使用顯式的'JOIN'運算符。如果該教程提倡超過20年的聯合風格,那麼可能需要找到一個不同的教程。 –

回答

0

FROM子句中,您可以確定要通過JOIN關鍵字加入哪些表,並使用ON關鍵字描述要共用的列。

SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
FROM tutorials_tbl a 
    JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author 

或者你可以使用USING,因爲共享的列名稱相同:

SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
FROM tutorials_tbl a 
    JOIN tcount_tbl b USING(tutorial_author) 
1

這是寫作的一個很老套的方式連接。

新的和時髦的方式是把它寫這樣的:

SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count 
FROM tutorials_tbl a 
INNER JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author 

要添加更多的表,你只需添加更多的JOIN子句。由於你的教程看起來有些過時,下面是我見過的最簡單,最美麗,最短的連接解釋:A Visual Explanation of SQL Joins

+0

+1鏈接到'JOIN'的視覺解釋。檢查圖表通常更容易。 –