MYSQL 计数 - 如果为空则返回 0

标签 mysql sql null

这是我的查询:

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/

相关文章:

mysql - 减去特定列的值,其中特定列的值在两个表中相同

SQL - 按别名上的 Max() 排序

sql - 如何 - 在 SQL Server 中创建后直接创建和使用数据库?

sql-server - 空值是否节省存储空间?

mysql - Select 语句结果带空格和不带空格

php - 在 Laravel 中为现有数据库中的存储过程、函数和事件创建迁移

mysql - IF/case如何选择inner join field?

php - mysql 语法说明 ' ' = ' '

sql-server - T-SQL IS NOT NULL 与 NOT NULL 性能

超大缓冲区中的 C 字符串空终止符