如果两个标准的结果相等,则选择一个值

时间:2014-04-02 10:38:05

标签: sql select

如果两个标准的结果相等,则从CODE行中选择值。 条件示例:

  

DAYS = 30,RATE 2   和   DAYS = 60,RATE 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)

但是第二部分......封装了吗?

结果应为01

有什么建议吗? thx提前。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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

答案 2 :(得分:0)

试试这个,

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