2014-04-02 50 views
0

如果兩個標準的結果相同,則從CODE行中選擇值。一個條件的 示例:如果兩個標準的結果相同,則選擇一個值

DAYS = 30,RATE 2 AND DAYS = 60,評分0

ID | CODE| DAYS| RATE| 
    1 | 01 | 30 | 2 | 
    2 | 05 | 20 | 3 | 
    3 | 01 | 60 | 0 | 
    4 | 06 | 30 | 3 | 
... 

第一部分是

SELECT * FROM Tablename WHERE (DAYS = 30 AND RATE = 2) 

但第二部分..encaps ulated?

結果應該是01

有什麼建議? thx提前。

回答

1

你需要一個自聯接:

SELECT DISTINCT t1.code 
FROM Tablename t1 
INNER JOIN Tablename t2 
ON t1.code = t2.code 
WHERE t1.days = 30 AND t1.rate = 2 
AND t2.days = 60 AND t2.rate = 0 
+0

thx很多!有用 –

0

試試這個,

SELECT * FROM Tablename WHERE (DAYS = 30 AND RATE = 2) OR (DAYS = 60 AND RATE = 0) 
1
SELECT DISTINCT CODE FROM Tablename 
WHERE (DAYS = 30 AND RATE = 2) 
OR (DAYS = 60 AND RATE = 0) 
+0

嘿朋友,我有沒有共享任何比這不同ANS? – AK47

+0

沒關係,因爲你們都誤解了這個問題。 – Twinkles

+0

@ AK47答案將導致兩個記錄從代碼'01',這是區別... – tetri

相關問題