0

我想获取 2 列计数并将它们的总数作为一个新列。我怎样才能做到这一点?

我写了这个查询,但这返回了错误的总数。

SELECT count(case when `status`='1' then 1 else 0 end) AS HOT,
count(case when `status`='5' then 1 end) 
AS Special_Case,count(case when 1=1 then 1 end) AS TOTAL 
FROM `tbl_customer_conversation` group by 
date(`dt_added`),user_id
4

1 回答 1

2

COUNT 只会给出匹配记录的时间,在您的查询中将始终返回 1。因为值可以是10。所以count(1)也是 1count(0)也是1

HOTAS,您想要案例总数,SPECIAL_CASE您必须使用 SUM。

SELECT 
    SUM(case when `status`='1' then 1 else 0 end) AS HOT,
    SUM(case when `status`='5' then 1 end) AS Special_Case,
    SUM(case when `status` = '1' or `status` = '5' then 1 end) AS TOTAL 
FROM `tbl_customer_conversation` 
group by date(`dt_added`),user_id
于 2012-04-18T10:16:48.950 回答