2012-04-18 60 views
0

我想获取2个coulmns数并将它们的总数作为新列。 我该怎么做?Mysql添加并将它们添加为新列

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

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 
+0

你是怎么意思把'把他们全部当成新专栏? – Starx 2012-04-18 10:12:33

+0

在HOT和Special_Case中获取的总人数为 – Thompson 2012-04-18 10:14:54

+0

'当1 = 1然后1结束时总会返回一个.. – Starx 2012-04-18 10:15:42

回答

2

COUNT只会给一个记录是匹配的时候,就是在你的查询将始终返回1,因为该值可以是10。所以count(1)也是1和count(0)也是1

AS,你想要的总数为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 
+0

phpmyadmin说“ 1305 - 功能lmsapi.SUM不存在“ – Thompson 2012-04-18 10:19:46

+0

@MohanSinfh,SUM不允许有空格。我修复了查询再次尝试。 – Starx 2012-04-18 10:21:50

+0

如果你已经解释了他为什么使用'SUM'而不是'count' – fancyPants 2012-04-18 11:24:07

相关问题