Mysql语句,如果至少有一行包含结果

时间:2014-08-29 06:39:30

标签: mysql select

我有一张记录表:

----------------------------------
ID   |  UniqueId |  Name  | Result
----------------------------------
1       1           Test1   OK
2       1           Test1   Cancelled
3       1           Test1   OK
4       2           Test2   OK
5       2           Test2   OK
6       2           Test2   OK
7       2           Test2   OK
8       3           Test3   OK
9       3           Test3   OK

让我们说我不想检查UniqueId = 1的至少一行是否包含Result == Cancelled。要排除UniqueId = 1的记录,因为它已被取消。 我怎样才能做到这一点? 谢谢

4 个答案:

答案 0 :(得分:0)

SELECT id FROM mytable WHERE uniqueId = 1 AND result != 'Cancelled'

答案 1 :(得分:0)

只需询问UniqueId = 1Result != Cancelled

的行
SELECT ID FROM table WHERE UniqueId = 1 AND Result <> 'Cancelled' LIMIT 1

答案 2 :(得分:0)

我是这样做的,但其他答案在语法方面要容易一些。

SELECT UniqueId, Name
FROM Table T
GROUP BY UniqueId, Name
HAVING Result != 'Cancelled'

答案 3 :(得分:0)

SELECT t1.* from table as t1 where t1.UniqueId not in(select t.UniqueId from table as t 
where t.Result="Cancelled")