目前我将新闻分组为月份+年,但我们没有足够的新闻文章。因此,他们希望按季节对它们进行分组:夏季,春季,秋季或冬季+年
所以而不是:
这将是:
每月archieve
SELECT MONTHNAME(news_date) AS MONTH,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS'
AND news_status = 'ENABLED'
GROUP BY CONCAT(MONTH(news_date), ' ', YEAR(news_date))
ORDER BY news_date DESC
答案 0 :(得分:3)
要获得数字季节(冬季为0至秋季为3),请尝试更紧凑:
SELECT quantity, FLOOR((MONTH(date_field) % 12)/3) as season
FROM `tbl`
GROUP BY season
答案 1 :(得分:2)
SELECT CASE WHEN MONTH(news_date) IN (12, 1, 2) THEN 'Winter'
WHEN MONTH(news_date) IN (3, 4, 5) THEN 'Spring'
WHEN MONTH(news_date) IN (6, 7, 8) THEN 'Summer'
ELSE 'Fall' END AS Season,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS'
AND news_status = 'ENABLED'
GROUP BY YEAR(news_date), Season
ORDER BY news_date DESC
答案 2 :(得分:1)
SELECT case when MONTH(news_date) between 3 and 5 then 'Spring',
when MONTH(news_date) between 6 and 8 then 'Summer',
when MONTH(news_date) between 9 and 11 then 'Autum',
when MONTH(news_date) >= 12 and MONTH(news_date) <= 2 then 'Winter'
end AS Period,
YEAR(news_date) AS YEAR,
COUNT(news_id) AS COUNT
FROM news
WHERE news_type = 'NEWS' AND news_status = 'ENABLED'
GROUP BY YEAR(news_date), Period
ORDER BY news_date DESC
答案 3 :(得分:0)
在查询中使用如下所示的CASE语句
CASE WHEN MONTH(news_date) = 1 or MONTH(news_date) = 2 or MONTH(news_date) = 3
THEN CONCAT('SPRING ' , YEAR(news_date) )
WHEN MONTH(news_date) = 4 or MONTH(news_date) = 5 or MONTH(news_date) = 6
THEN CONCAT('SUMMER ' , YEAR(news_date) )
WHEN MONTH(news_date) = 7 or MONTH(news_date) = 8 or MONTH(news_date) = 9
THEN CONCAT('AUTUMN ' , YEAR(news_date) )
WHEN MONTH(news_date) = 10 or MONTH(news_date) = 11 or MONTH(news_date) = 12
THEN CONCAT('WINTER ' , YEAR(news_date) )