1

目前我将新闻分组为月+年,但我们没有足够的新闻文章。因此,我想按季节对它们进行分组:夏季、春季、秋季或冬季 + 年

所以而不是:

  • 2013 年 1 月
  • 2013 年 3 月
  • 2013年4月

这将是:

  • 夏天2013
  • 2013年春季

每月档案

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
4

4 回答 4

3

To get numeric seasons (Winter as 0 through to Autumn as 3), try the more compact:

SELECT quantity, FLOOR((MONTH(date_field) % 12)/3) as season
FROM `tbl`
GROUP BY season
于 2014-07-20T12:05:20.257 回答
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
于 2013-10-30T10:56:01.980 回答
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
于 2013-10-30T10:56:53.393 回答
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) ) 
于 2013-10-30T10:56:10.240 回答