1
所以,我有兩個表,其中一個是這樣的:Mysql的,內部連接在多個列具有相同的名稱
表:data
id | col_1 | col_2 | col_3 | col_4 ------------------------------------------------------ 1 | 167 | 12 | 15 | something1 2 | 198 | 27 | 12 | something2 3 | 253 | 15 | 17 | something3
和表:catalog
id | col_1 ----------------- 12 | red 15 | yellow 17 | pink 27 | green
現在,我想要做的是從獲取col_1
,col_2
,col_3
和表,但是根據catalog
id
,而不是獲取col_2
和col_3
的值,但應將這些值替換爲catalog
表的col_1
。
例如,我希望它回來,爲數據表,下列信息中的每一行:
167 | red | yellow | something1 198 | green | red | something2 253 | yellow | pink | something3
我嘗試這樣做:
SELECT data.col_1, catalog.col_1, catalog.col_1, data.col_4
FROM data
INNER JOIN catalog ON data.col_2 = catalog.id
INNER JOIN catalog ON data.col_3 = catalog.id
但毫無效果和驚喜。
我簡直無法找到如何將一個表的多個列內連接到另一個表的一列,而Google一直沒有很大的幫助。我實際上不知道我是否正在使用正確的關鍵字進行搜索。
您必須使用過時的谷歌版本。這是SQL 101.'catalog'的每個實例都需要自己的別名。 – Strawberry
但是如果'data'中的一個列有一個id不存在於'catalog'中會發生什麼? –
@Strawberry也許他使用Bing。 – Mihai