mysql有个字段是DATETIME类型,我可以按月统计么,该怎么写sql语句?
...group by 月份
该怎么写?

解决方案 »

  1.   

    select month(f1) from tt group by month(f1)
    or
    select DATE_FORMAT(f1,'%m')  from tt group by DATE_FORMAT(f1,'%m')
      

  2.   

    select DATE_FORMAT(时间字段, '%Y-%c') as my_month from 表 group by my_month
      

  3.   

    select DATE_FORMAT(时间字段, '%Y-%m') as my_month from 表 group by my_month 
      

  4.   

    比如数据库的为2008-01-15 12:10:00
    则DATE_FORMAT的参数格式分别得到的结果为:
    '%Y'  2008
    '%Y-%m'  2008-01
    '%Y-%c'  2008-1
    '%m'  01
    '%c'  1
      

  5.   

    如果考虑所月,用DATE_FORMAT,否则用MONTH就行了
      

  6.   


    select * from tablename where 1 group by date_format(field1,'%Y%m');