2012-09-12 19 views
2

下面的代碼不工作,但我使用的是什麼,我試圖完成一個例子:選擇使用,其中在一個子查詢的多個元素

SELECT thing_id 
FROM table_of_things 
WHERE table_of_things.thing_color, table_of_things.thing_flavor IN 
          (SELECT good_color,good_flavor FROM good_things) 

編輯:所以顯然它從wasent明顯我代碼給一些人。我正嘗試使用子查詢中的兩個條件從table_of_things中獲取things_id。

+0

什麼是你的問題? –

+0

你需要更具體地瞭解你正在努力完成什麼。 –

+0

嘗試使用兩個表之間的連接.....雖然你需要解釋你的問題多一點.. :) –

回答

6

你還沒有問一個問題,但我懷疑你需要查詢更改爲以下:

SELECT thing_id 
FROM table_of_things 
WHERE table_of_things.thing_color IN (SELECT good_color FROM good_things) 
AND table_of_things.thing_flavor IN (SELECT good_flavor FROM good_things) 

或可能:

SELECT thing_id 
FROM table_of_things as tot 
INNER JOIN good_things as gt 
ON tot.thing_color = gt.good_color AND tot.thing_flavor = gt.good_flavor 

的聲明中只能查詢一個值,不像你想要做的那樣。

+0

謝謝。我認爲IN只能使用1,想看看我是否錯過了一些明顯的東西。 –

1

使用加入

SELECT t.thing_id 
FROM table_of_things t 
JOIN good_things gt 
ON t.thing_color = gt.good_color 
AND t.thing_flavor = gt.good_flavor 

使用聯盟

SELECT t.thing_id FROM table_of_things t WHERE t.thing_color IN (SELECT good_color FROM good_things) 
UNION 
SELECT t.thing_id FROM table_of_things t WHERE t.thing_flavor IN (SELECT good_flavor FROM good_things) 

使用EXIST

SELECT t.thing_id FROM table_of_things t 
WHERE EXISTS (SELECT good_color,good_flavor FROM good_things) 
相關問題