这是我的查询:
select COUNT(*) as total, COUNT(distinct user) as unique
FROM table
WHERE uristem in ('/example/', '/example/', '/example/', '/example/')
and time > DATE_SUB(NOW(), INTERVAL 24 HOUR)'
group by uristem;
应该是这样的
100 50
25 50
0 0
100 35
但是如果没有计数,它看起来像这样(缺失)
100 50
40 50
100 35
如何填写 0?
最佳答案
试试这个(未经测试):
select COUNT(t.uristem) as total
, COUNT(distinct t.user) as unique
FROM
(
select uristem, user
from table
where time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
) t
right outer join
(
select '/example/' x
union select '/example/'
union select '/example/'
union select '/example/'
) y
on t.uristem = y.x
group by y.x;
关于MYSQL 计数 - 如果为空则返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353624/