我正在使用MySQL將兩個不同的表連接在一起。 people
和homes
。爲什麼SELECT * FROM table INNER JOIN..ON顯示相交列兩次?
當我嘗試內加入這兩個與USING
一起進行,它給回相交的列(地址)一列我想加入:
SELECT * FROM people INNER JOIN homes USING(address);
+---------------------+------------+-----------+----------+
| address | first_name | last_name | city |
+---------------------+------------+-----------+----------+
| 533 Dufferin Street | Joe | Smith | Toronto |
| 421 Yonge Street | John | Schmidt | New York |
| 90 Bayview Avenue | Mary | Poppins | Chicago |
| 800 Keele Street | Joe | Dirt | L.A |
+---------------------+------------+-----------+----------+
然而,當你內部使用ON
關鍵字連接兩個表格,它爲address
(相交列)返回兩列。
SELECT * from people INNER JOIN homes ON(people.address = homes.address);
+------------+-----------+---------------------+---------------------+----------+
| first_name | last_name | address | address | city |
+------------+-----------+---------------------+---------------------+----------+
| Joe | Smith | 533 Dufferin Street | 533 Dufferin Street | Toronto |
| John | Schmidt | 421 Yonge Street | 421 Yonge Street | New York |
| Mary | Poppins | 90 Bayview Avenue | 90 Bayview Avenue | Chicago |
| Joe | Dirt | 800 Keele Street | 800 Keele Street | L.A |
+------------+-----------+---------------------+---------------------+----------+
所以我想總結一下,爲什麼USING
結果列address
顯示一次,導致address
與ON
被顯示兩次?
可能重複[MySQL ON vs USING?](http://stackoverflow.com/questions/11366006/mysql-on-vs-using)...它沒有深刻的解釋爲什麼,但我認爲它只是爲了讓事情變得更簡單,因爲USING無論如何都是語法糖 – dan08
它不是重複的。這就要求MySQL ON與使用之間的通用差異。我要問一個爲什麼兩個輸出不同結果的具體實例。雖然第一個答案指出select * FROM INNER JOIN ON會導致列出兩次,但並不能解釋原因。 – the12
真的嗎?你讀過羅伯特羅查的回答,那對你不滿意? – dan08