[英]MySQL count with case return wrong count
我有以下问题:
我有mysql表:
continent,
country,
city,
zip,
street,
status (int),
reason (varchar, can be empty or some text),
... (some other columns, not required for query)
but with mapping of status int to text: 我想按来计数每种状态的出现但要将状态int映射到文本:
SELECT country,city,zip,street,
CASE WHEN (status = '2' AND reason <> '') THEN "BLOCKED"
WHEN (status = '2' AND reason='') THEN "DISPUTED"
WHEN status = '3' THEN 'EXPIRED'
WHEN status = '1' THEN 'ACTIVE' ELSE 'UNKNOWN'
END as status,
count(*) AS count
FROM people
where continent='Europe'
GROUP BY country,city,zip,street,status
我认为问题出在GROUP BY,但不确定-它不会返回正确的数据。
SQLFiddle (检查Paris / Street2-它显示3 DISPUTED,并且应该显示1 BLOCKED和2 DISPUTED)
感谢您的意见。
我认为EngineerCoder 打算写 :
select country,city,zip,street, status, count(*)
from
(
SELECT country,city,zip,street,
CASE WHEN (status = '2' AND reason <> '') THEN "BLOCKED"
WHEN (status = '2' AND reason='') THEN "DISPUTED"
WHEN status = '3' THEN 'EXPIRED'
WHEN status = '1' THEN 'ACTIVE' ELSE 'UNKNOWN'
END as status,
FROM people
where continent='Europe'
) AS ilv
GROUP BY country,city,zip,street,status
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.