Mysql 如何在case语句中添加内容

Mysql 如何在case语句中添加内容,mysql,Mysql,我的问题如下 select date(created_at), count(distinct updated = '3' then user_id end) as unique, count(distinct updated <> '3' then user_id end) as duplicate1, count(distinct updated_by <> '4' then user_id end) as duplicate2 from us

我的问题如下

select date(created_at), 
  count(distinct  updated = '3'  then user_id end) as unique, 
  count(distinct  updated <> '3' then user_id end) as duplicate1,
  count(distinct  updated_by <> '4' then user_id end) as duplicate2

from user
where date(created_at) between current_date-2 and current_date
group by date(created_at) 
选择日期(创建时),
计数(不同的更新='3'然后用户id结束)为唯一,
计数(不同的更新“3”,然后用户id结束)为重复项1,
计数(由“4”进行不同的更新,然后用户id结束)为重复项2
来自用户
其中日期(创建日期)介于当前日期-2和当前日期之间
按日期分组(创建时间)

我还需要一个列,上面写着“重复总数”,它是重复1和重复2的总和

只需添加一个新的条件计数,其中包括两个重复条件:

SELECT
    DATE(created_at),
    COUNT(DISTINCT updated = '3' THEN user_id END) AS unique, 
    COUNT(DISTINCT updated <> '3' THEN user_id END) AS duplicate1,
    COUNT(DISTINCT updated_by <> '4' THEN user_id END) AS duplicate2
    COUNT(DISTINCT updated <> '3' OR updated_by <> '4'
              THEN user_id END) AS total_duplicates
FROM user
WHERE
    DATE(created_at) BETWEEN CURRENT_DATE - 2 AND CURRENT_DATE
GROUP BY
    DATE(created_at);
选择
日期(创建时间),
计数(不同的更新='3'然后用户id结束)为唯一,
计数(不同的更新“3”,然后用户id结束)为重复项1,
计数(由“4”进行不同的更新,然后用户id结束)为重复项2
计数(分别更新了“3”或更新了“4”
然后用户id(结束)作为总重复项
来自用户
哪里
当前日期-2和当前日期之间的日期(创建日期)
分组
日期(创建于);

如果要计算由“3”更新的
行和由“4”更新的
行,则需要条件:

count(distinct case when updated_by <> '3' and updated_by <> '4' then user_id end)
因此,您的代码应该是:

select date(created_at), 
  count(distinct case when updated_by = '3'  then user_id end) as unique, 
  count(distinct case when updated_by <> '3' then user_id end) as duplicate1,
  count(distinct case when updated_by <> '4' then user_id end) as duplicate2,
  count(distinct case when updated_by not in ('3', '4') then user_id end) as total_duplicates
from user
where date(created_at) between current_date-2 and current_date
group by date(created_at)
select date(created_at), 
  count(distinct case when updated_by = '3'  then user_id end) as unique, 
  count(distinct case when updated_by <> '3' then user_id end) as duplicate1,
  count(distinct case when updated_by <> '4' then user_id end) as duplicate2,
  count(distinct user_id) as total_duplicates
from user
where date(created_at) between current_date-2 and current_date
group by date(created_at)
select date(created_at), 
  count(distinct case when updated_by = '3'  then user_id end) as unique, 
  count(distinct case when updated_by <> '3' then user_id end) as duplicate1,
  count(distinct case when updated_by <> '4' then user_id end) as duplicate2,
  count(distinct user_id) as total_duplicates
from user
where date(created_at) between current_date-2 and current_date
group by date(created_at)