這是在那一刻滑落我的腦海裏獲得額外的列,讓我們說,我有這樣的代碼:MySQL的子查詢的WHERE IN,但也從IN子
SELECT * FROM Table1
WHERE (FoundCity, FoundState) IN
(SELECT city, state, distance
FROM Table2
) ;
我怎麼也弄它拉的距離上面的'主'查詢列?
這是在那一刻滑落我的腦海裏獲得額外的列,讓我們說,我有這樣的代碼:MySQL的子查詢的WHERE IN,但也從IN子
SELECT * FROM Table1
WHERE (FoundCity, FoundState) IN
(SELECT city, state, distance
FROM Table2
) ;
我怎麼也弄它拉的距離上面的'主'查詢列?
將子查詢更改爲聯接。然後您可以從任一表中選擇任何列。
SELECT *, t2.distance
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.FoundCity = t2.city AND t1.FoundState = t2.state
編輯
如果你想保持元組連接語法,你也可以這樣做:
SELECT t1.*, t2.distance
FROM t1 INNER JOIN t2
ON (t1.FoundCity, t1.FoundState) = (t2.city, t2.state);
非常感謝,這工作完美! – Bryant
SELECT * FROM Table1 as T1
LEFT JOIN Table2 as T2 on T1.FoundCity = T2.FoundCity AND T1.FoundState = T2.FoundState
更好的主意是使用左連接。在*中,您可以包含使用T1作爲表1的列的前綴和表2的列的前綴名稱。(例如:T2.state)
來看一下,[子查詢(HTTPS:/ /dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html) –