2016-10-10 1 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 | 
----------------- 

は、所望の出力です:タグmysqliによって

----------------- 
| 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

あなたは本当にグループの後にHaving節を持っていませんか? –

+1

明白ですが必要な質問:これらの行はすべて実際にDBに存在するのですか、このクエリのために生成することを望んでいますか? – 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

私はあなたが「と仮定することができます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をLEFTのためのすべての年月の組み合わせを含むテーブルを必要とします。あなたはすべての年と月を結ぶクロスによってその場でそれを作成することができます

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? date_year'または 'SELECT $ year as date_year'を返します。 –

関連する問題