2016-02-02 97 views
0

表1:MySQL的選擇一對多

PRP_ID |  NAME  
---------------------- 
100  | something_100   
101  | something_101   
102  | something_102 

表2:

ADN_ID |  NAME  
---------------------- 
11  | thing_11   
22  | thing_22   
33  | thing_33   

表3:

ID |  ADN_ID | PRP_ID | VALUE 
---------------------------------------- 
1 |  11  | 100 | YES 
2 |  22  | 100 | YES 
3 |  11  | 101 | NO 
4 |  22  | 101 | YES 
5 |  33  | 102 | YES 

這是我的數據庫結構URE。 我想從過去的表中選擇PRP_ID其中ADN_IDIN(11,22)WHERE VALUE = YES兩個ADN_ID

它應該通過很簡單,但我不知道該怎麼做了。


SELECT PRP_ID 
FROM table3 
WHERE ADN_ID IN(11,22) 
AND VALUE = YES 
GROUP BY PRP_ID 

這是行不通的,因爲它只會檢查ADN_ID 11值

回答

2

您可以添加having條款:

SELECT PRP_ID 
FROM table3 
WHERE ADN_ID IN (11,22) AND VALUE = 'YES' 
GROUP BY PRP_ID 
HAVING COUNT(DISTINCT ADN_ID) = 2; 
+0

那是什麼我包換! :) 謝謝 – Gemmi