2016-05-22 107 views
-2

考慮到下面的MySQL語句:附加列導致

SELECT name FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576)) 

執行時,它給了我下面的結果:

enter image description here

但我想包括來自IDS job_description列內部選擇語句作爲第一列。這裏的內部SELECT語句的結果:

enter image description here

我嘗試使用第二個SELECT語句的別名,但沒有奏效。

回答

1

你想要的列修改您的查詢像

SELECT t2.id, jtd.name 
FROM jobtype_descriptions jtd 
JOIN table2 t2 ON jtd.id = t2.job_description 
WHERE t2.id IN (10684,10580,10583,10582,10581,10579,10576); 
+0

我編輯了我的問題 –

1

您需要包括的名字即

SELECT id, name FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576)) 
+0

我編輯了我的問題 –

1

嘗試

SELECT name, id FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576)) 

您通常可以包括你的任何列希望通過名稱引用它們來進行選擇。例如,SELECT col1, col2, col3 FROM ...

+0

我編輯了我的問題 –

+0

謝謝!如果我想將這些10684,10580,10583,10582,10581,10579,10576作爲一個列添加呢? –