Mysql 如何检查组中是否存在一个值,另一个值是否为';T
我想检查列的一个值是否存在于组中,而另一个不存在。我的桌子看起来像这样:Mysql 如何检查组中是否存在一个值,另一个值是否为';T,mysql,Mysql,我想检查列的一个值是否存在于组中,而另一个不存在。我的桌子看起来像这样: num | text | 1 | A | 1 | B | 2 | A | 2 | C | 3 | A | 3 | D | 4 | B | 4 | C | 我想获得数量为2,3的组。现在我使用以下查询: select c1.num from table c1 inner join table c2 on c1.num = c
num | text |
1 | A |
1 | B |
2 | A |
2 | C |
3 | A |
3 | D |
4 | B |
4 | C |
我想获得数量为2,3的组。现在我使用以下查询:
select c1.num from table c1 inner join table c2 on c1.num = c2.num
where c2.text !='B' and c1.type !='B'
group by c1.num, c1.text
having(c1.text='A');
我试过用一个辅助选择
select c1.num from table as c1
where c1.text ='A'
and c1.num not in (select num from table where text='B');
我将把这两个条件都放在
HAVING
子句中,并结合案例,当构造:
select num
from table
group by num
having max(case text
when 'A' then 1
when 'B' then 2
end) = 1
或者换句话说:
select num
from table
group by num
having max(case when text='A' then 1 else 0 end) = 1
and max(case when text='B' then 1 else 0 end) = 0;
这种方法可能比两次选择表(子查询或联接)的查询效率更高。group by用于聚合函数,如count或max。。。您的意思是“我想检查列的一个值是否存在于组中,而另一个值是否存在?”?你的意思是如果这些值在一个集合中?而其他人不是吗?显示要获取的结果tooI want receive groups-groupby num,其中列“text”的值等于“A”,其他任何列不含“B”。结果应该是:2,3。