如何选择没有特定列值的项目?

时间:2013-12-11 15:34:11

标签: mysql

我的数据如下:

group_id | meta_key | meta_value
----------------------------------------
    33   | has_foo  | definitely_has_foo
    33   | is_bar   | might_be_bar
    33   | a_key    | a_value
    65   | has_foo  | probably
    86   | something| something_else

我想选择的所有group_id meta_key名为has_foo,在这种情况下为86 。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:3)

select group_id
from your_table
group by group_id
having sum(meta_key = 'has_foo') = 0

答案 1 :(得分:1)

SELECT group_id 
FROM T
WHERE group_id NOT IN (SELECT group_id FROM T WHERE meta_key = 'has_foo')