MySQL Group By Dates Between

前端 未结 3 1652
醉梦人生
醉梦人生 2020-12-21 09:18

Is there a way to group records that fall between two dates?

For example, my table has records that look like this:

rid     stamp                   u         


        
3条回答
  •  臣服心动
    2020-12-21 09:49

    If there were only one group then you could use the "group by with having clause". I don't think there is syntax to specify multiple different groupings in one statement and separate them though. Here is the simplest alternative solution I can think of that will work:

    select "July 2005 - January 2006" AS "Date", count(date) as "results" 
    from MYTABLE 
    where date >= '2005-07-01' AND date <= '2006-01-31'
    
    union
    
    select "February 2006 - August 2006" AS "Date", count(date) as "results" 
    from MYTABLE 
    where date >= '2006-02-01' AND date <= '2006-08-31';
    

提交回复
热议问题