2012-07-30 108 views
2

我在flot中創建一個線圖,除了沒有結果的日子之外,我已經把它全部工作了,我需要它們返回結果0 +日期。這可能在MySQL?這是我目前的查詢:MySQL查詢得到0如果沒有結果的日期

$chartQuery = "SELECT count(date) as counted_leads, UNIX_TIMESTAMP(date) as time FROM enquiries WHERE visibility != 'deleted' group by date"; 

或者我需要在我的PHP?這裏是我的代碼:

<?php 
     $last_key = end(array_keys($chartResults)); 
     foreach ($chartResults as $item => $value) 
     { 
      $timestamp = round($value['time'] * 1000); 

      if ($item == $last_key) 
      { 
       // last element 
       echo '['.$timestamp.', '.htmlentities($value['counted_leads']).']'; 
      } 
      else 
      { 
       // not last element 
       echo '['.$timestamp.', '.htmlentities($value['counted_leads']).'],'; 
      } 
     } 
     unset($value); 
     ?> 
+0

您可以加入一個日期/時間表,但在PHP中做它可能實際上更容易。 – 2012-07-30 05:55:34

+0

我已更新我的帖子嘗試它。 – Omesh 2012-07-30 06:27:13

+0

如果您的查詢未返回任何行,您將使用哪個日期? – Mark 2012-07-30 13:25:48

回答

0

我認爲這是你在找什麼:

SELECT time, SUM(counted_leads) AS counted_leads 
FROM(
     SELECT UNIX_TIMESTAMP(date) AS time, count(1) AS counted_leads 
     FROM enquiries 
     WHERE visibility != 'deleted' 
     GROUP BY time 

     UNION ALL 

     SELECT a.Date AS time, 0 AS counted_leads 
     FROM (
      SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date 
      FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a 
      CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b 
      CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c 
     ) a 
     WHERE a.Date BETWEEN (SELECT MIN(date) FROM enquiries) AND (SELECT MAX(date) FROM enquiries) 
) a 
GROUP BY time; 
+0

我已經添加了這個,但它似乎不傳回0行? – neoszion 2012-07-30 12:38:35

+0

嘗試直接在UNION ALL的服務器上執行單個查詢。 – Omesh 2012-07-30 12:41:39

+0

我不熟悉UNION ALL語法?我將如何執行? – neoszion 2012-07-30 17:21:15

2
SELECT 
    ifnull(count(date),0) as counted_leads, 
    UNIX_TIMESTAMP(date) as time 
FROM enquiries WHERE visibility != 'deleted' 
group by date 

使用,如果空的defult值

+1

這是行不通的。因爲沒有這樣的行。 – CyberDem0n 2012-07-30 06:00:02