MySQL/SQL: Group by date only on a Datetime column

前端 未结 4 1015
逝去的感伤
逝去的感伤 2020-11-28 18:26

Having a table with a column like: mydate DATETIME ...

I have a query such as:

SELECT SUM(foo), mydate FROM a_table GROUP BY a_table.myd         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 18:57

    Cast the datetime to a date, then GROUP BY using this syntax:

    SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate);
    

    Or you can GROUP BY the alias as @orlandu63 suggested:

    SELECT SUM(foo), DATE(mydate) DateOnly FROM a_table GROUP BY DateOnly;
    

    Though I don't think it'll make any difference to performance, it is a little clearer.

提交回复
热议问题