【问题标题】:Mysql addition and add them as new columnMysql添加并将它们添加为新列
【发布时间】:2012-04-18 10:10:58
【问题描述】:

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

【问题讨论】:

  • do their total as new column 是什么意思?
  • HOT 和 Special_Case 中提取的数字总数
  • case when 1=1 then 1 end 将始终返回一个..
  • 是的,但我想计算 HOT 和 Special_Case 的数量,然后是总数

标签: php mysql count case


【解决方案1】:

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

【讨论】:

  • phpmyadmin 说“#1305 - 函数 lmsapi.SUM 不存在”
  • @MohanSinfh,SUM 不允许使用空格。我修复了查询再试一次。
  • 如果你向他解释了为什么使用SUM而不是COUNT,我会赞成这个答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
相关资源
最近更新 更多