select 记录 group_concat() 包含一个字符

select record that group_concat() contain a char

如何selectmystatus字段只包含0

select c.id,count(*),
       group_concat(distinct(r.status not in ('Done','None'))) as mystatus
from cases c inner join reports r 
on (c.id = r.parent_id and r.parent_type = 'Cases' and r.deleted = 0)
where c.deleted = 0 
and c. status <> 'Late'
group by c.id

结果看起来像这样,但我想 select 因为只有 mystatus 包含 0。

ID  count(*)  mystatus
A   1         0
B   7         0,1
C   2         0

应该是select记录ID A和C

您必须使用 HAVING 子句:

select c.id,count(*),
       group_concat(distinct(r.status not in ('Done','None'))) as mystatus
from cases c inner join reports r 
on (c.id = r.parent_id and r.parent_type = 'Cases' and r.deleted = 0)
where c.deleted = 0 and c. status <> 'Late'
group by c.id
having count(case when r.status <> 0 then 1 end) = 0

HAVING子句的谓词使用条件聚合过滤掉c.id组包含至少一个r.status <> 0.

记录

注意:如果r.status字段是字符类型,则使用r.status <> '0'

添加 HAVING 子句。

select c.id,count(*),
       group_concat(distinct(r.status not in ('Done','None'))) as mystatus
from cases c inner join reports r 
on (c.id = r.parent_id and r.parent_type = 'Cases' and r.deleted = 0)
where c.deleted = 0 
and c. status <> 'Late'
group by c.id
having mystatus = '0'