Mysql 添加并将它们添加为新列


Mysql addition and add them as new column

我想获取 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

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

例如,您需要HOT事例的总数,并且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