如何在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 :(得分: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 :(得分:1)
您需要一个包含LEFT 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
答案 2 :(得分: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