簡體   English   中英

MYSQL計數 - 如果為null則返回0

[英]MYSQL Count - Return 0 if 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?

如果你沒有一個帶有uri莖的表,那就是丑陋的東西

Select
  u.uristem,
  count(t.uristem),
  count(distinct t.user) as unique
From (
    Select '/example1/' As uristem
    Union All Select '/example2/'
    Union All Select '/example3/'
    Union All Select '/example4/'
  ) u
    Left Outer Join
  table t
    On u.uristem = t.uristem And
    t.time > Date_Sub(Now(), Interval 24 Hour)
 Group By
   u.uristem

應該這樣做

試試這個(未經測試):

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM