查询以获取不同的值

时间:2013-06-14 21:31:07

标签: mysql sql

表 - 订单

columnA 1 1 2 3 4 4 5

columnB 80 20 80 80 80 20 80

不应检索columnB = 20的columnA,需要如下结果。

columnA 2 3 5

columnB 80 80 80

先谢谢你的帮助......

3 个答案:

答案 0 :(得分:1)

试试这个

 select * from `order` where c1 not in (select c1 from `order` where c2 = 20)
 group by c1

**注意:order是mysql保留关键字,所以你应该通过反引号来逃避它

DEMO HERE

C1  C2
2   80
3   80
5   80

答案 1 :(得分:0)

尝试NOT IN:

SELECT *
FROM table
WHERE C1 NOT IN (SELECT C1
                 FROM table
                 WHERE c2 = 20)

答案 2 :(得分:0)

尝试此查询:

SELECT DISTINCT o.c1, o.c2
FROM `order` o
WHERE NOT EXISTS (
    SELECT 1
    FROM `order` o2
    WHERE o.c1 = o2.c1
      AND o2.c2 = 20
)
;