2016-10-10 31 views
0

如何獲取在GROUP BY語句中顯示的所有行?如何顯示GROUP BY語句中的所有行?

下面是該查詢:

SELECT year(feed_date) as date_year, 
     month(feed_date) as date_month, 
     count(*) as nb_item 
FROM table 
WHERE year(feed_date) = '2015' AND 
     feed_title LIKE '%word%' AND 
     source = '3' 
GROUP BY date_year, date_month 

這裏是輸出:

----------------- 
| 2015 | 7 | 5 | 
| 2015 | 9 | 2 | 
| 2015 | 10 | 4 | 
| 2015 | 11 | 2 | 
----------------- 

下面是所需的輸出:

----------------- 
| 2015 | 1 | 0 | 
| 2015 | 2 | 0 | 
| 2015 | 3 | 0 | 
| 2015 | 4 | 0 | 
| 2015 | 5 | 0 | 
| 2015 | 6 | 0 | 
| 2015 | 7 | 5 | 
| 2015 | 8 | 0 | 
| 2015 | 9 | 2 | 
| 2015 | 10 | 4 | 
| 2015 | 11 | 2 | 
| 2015 | 12 | 0 | 
----------------- 
+0

你確定,你沒有組條件嗎? –

+1

明顯但必要的問題:所有這些行實際上是否存在於數據庫中,或者您是否希望爲了查詢的目的而生成這些行? – ADyson

+0

我想有些月份只是不在原表中。 –

回答

1

你可以加入選擇數據與子查詢獲取表中所有現有年份和月份。

SELECT t1.date_year, t1.date_monthmonth, IFNULL(t2.nb_item, 0) AS nb_item 
FROM (SELECT DISTINCT YEAR(feed_date) AS date_year, MONTH(feed_date) AS date_month 
     FROM table) AS t1 
LEFT JOIN (
    SELECT year(feed_date) as date_year, 
      month(feed_date) as date_month, 
      count(*) as nb_item 
    FROM table 
    WHERE year(feed_date) = '2015' AND 
      feed_title LIKE '%word%' AND 
      source = '3' 
    GROUP BY date_year, date_month) AS t2 
ON t1.date_year = t2.date_year AND t1.date_month = t2.date_month 
ORDER BY t1.date_year, t1.date_month 
+0

我收到此錯誤消息: #1054 - '字段列表'中的未知列't2.count' 我將'date_monthmonth'更正爲'date_month'和'MONT(feed_date)'更改爲'MONTH(feed_date)''。但仍然沒有工作... – Guillaume

+0

這應該是'nb_item' – Barmar

+0

謝謝,它的工作。 :) – Guillaume

2

按標籤mysqli我可以假設你」重新使用PHP。

所以這是可以做到這樣:

$year = '2015'; 
$data = []; 
foreach(range(1, 12) AS $month) { 
    $data[$month] = [ 
    'date_year' => $year, 
    'date_month' => $month, 
    'nb_item' => 0 
    ]; 
} 

$q = "SELECT 
     year(feed_date) as date_year, 
     month(feed_date) as date_month, 
     count(*) as nb_item 
     FROM table 
     WHERE year(feed_date) = '".$year."' 
     GROUP BY date_year, date_month"; 
$q = mysqli_query($q); 
while($record = mysqli_fetch_assoc($q)) { 
    $data[$record['date_month']]['nb_item'] = $record['nb_item']; 
} 

$data = array_values($data); 
print_r($data); 

或與MySQL這將是巨大的查詢:

SELECT 
    year(table.feed_date) AS date_year, 
    month(table.feed_date) AS date_month, 
    COALESCE(count(*), 0) as nb_item 
FROM (
    SELECT 1 as month 
    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 
    UNION ALL SELECT 10 
    UNION ALL SELECT 11 
    UNION ALL SELECT 12 
) months 
LEFT JOIN table ON (months.month = month(table.feed_date)) 
WHERE year(table.feed_date) = '2015' 
GROUP BY date_year, date_month; 
1

你需要包含所有的左邊的年月組合JOIN表。您可以通過交叉連接所有年份和月份在飛行創建:

SELECT y.date_year, m.date_month, count(*) as nb_item 
FROM (
    SELECT 1 as date_month 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 UNION ALL 
    SELECT 10 UNION ALL 
    SELECT 11 UNION ALL 
    SELECT 12 
) m 
CROSS JOIN (
    SELECT 2015 as date_year 
) y 
LEFT JOIN `table` t 
    ON year(t.feed_date) = y.date_year 
    AND month(t.feed_date) = m.date_month 
    AND t.feed_title LIKE '%word%' 
    AND t.source = '3' 
GROUP BY y.date_year, m.date_month 

如果你有序列號的輔助表可以縮短查詢:

SELECT y.seq as date_year, m.seq as date_month, count(*) as nb_item 
FROM sequences y 
CROSS JOIN sequences m 
LEFT JOIN `table` t 
    ON year(t.feed_date) = y.date_year 
    AND month(t.feed_date) = m.date_month 
    AND t.feed_title LIKE '%word%' 
    AND t.source = '3' 
WHERE y.seq IN (2015) 
    AND m.seq <= 12 
GROUP BY y.seq, m.seq 
+0

您寫過的比我快:D – num8er

+0

感謝您的回答,但年份也是一個變量,而不是常數。所以,我不知道如何修改CROSS JOIN語句中的查詢... – Guillaume

+0

@Guillaume在這個例子中,對於MySQL來說這一年是不變的,就像它在你的問題中一樣。你可以使用'SELECT? as date_year'或'SELECT $ year as date_year'。 –