MYSQL - 如何显示0是否没有结果?

时间:2015-02-06 10:57:50

标签: mysql sql

我有以下查询工作正常,并在仪表板上显示任何未接来电。但是,如果没有结果,则不返回任何数据。如何显示'0'是否没有结果?感谢

SELECT DialledNumber, COUNT(*) As Missed
FROM CALLS
WHERE destination like '%!%' AND datetime BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 day)
GROUP By DialledNumber
HAVING (DialledNumber = '500') OR (DialledNumber = '580') OR (DialledNumber = '515') OR (DialledNumber = '513') OR (DialledNumber = '514')

3 个答案:

答案 0 :(得分:0)

您可以在MYSQL中使用IFNULL函数

SELECT DialledNumber, IFNULL(COUNT(DialledNumber),0) As Missed
FROM CALLS
WHERE destination like '%!%' AND datetime BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 day)
GROUP By DialledNumber
HAVING (DialledNumber = '500') OR (DialledNumber = '580') OR (DialledNumber = '515') OR (DialledNumber = '513') OR (DialledNumber = '514')

答案 1 :(得分:0)

您想使用left join而不是in

SELECT dn.dn, COUNT(c.diallednumber) As Missed
FROM (select '500' as dn union all select '580' union all select '515' union all
      select '513' union all select '514'
     ) dn left join
     CALLS c
     ON c.diallednumber = dn.dn and
        c.destination like '%!%' AND
        c.datetime BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 day)
GROUP By dn.dn;

作为一个小小的音符,在英语中,单词"拨打"没有两个" l" s。

答案 2 :(得分:0)

这可能对您有用(未经测试):

SELECT CALLS.DialedNumber, IFNULL(MISSEDCALLS.Missed, 0) FROM CALLS
LEFT JOIN (
  SELECT DialedNumber, COUNT(*) As Missed FROM CALLS
  WHERE destination like '%!%' AND datetime BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 day) 
  GROUP By DialledNumber
) AS MISSEDCALLS ON MISSEDCALLS.DialedNumber = CALLS.DialedNumber
WHERE CALLS.DialledNumber IN ('500', '580', '515', '513', '514')