我在MySQL中有以下数据:
+------------+-------+
| myDATE | delta |
+------------+-------+
| 2015-08-29 | 12 |
| 2015-08-30 | 12 |
| 2015-08-31 | 10 |
| 2015-09-01 | 0 |
| 2015-09-02 | 0.15 |
+------------+-------+
我想运行一个查询,选择每个月的最后一天并显示数据。我以为我有它工作,但它没有显示昨天的数据。
select mydate, delta from data group by date_format(mydate, '%Y-%m');
结果:
+------------+-------+
| mydate | delta |
+------------+-------+
| 2015-08-29 | 12 |
| 2015-09-01 | 0 |
+------------+-------+
我错过了什么?
我正在寻找它来回复:(即每个月的最后一天有记录)
+------------+-------+
| myDATE | delta |
+------------+-------+
| 2015-08-31 | 10 |
| 2015-09-02 | 0.15 |
+------------+-------+
答案 0 :(得分:2)
select last_day(mydate), sum(delta) from data group by last_day(mydate)
last_day是一个函数,它返回给定日期的月份的最后一天
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day
回答更新后的问题
select data.`myDate`, data.delta from data inner join
(select max(`myDate`) myDate from data group by date_format(mydate, '%Y-%m')) a
on a.myDate = data.myDate