简体   繁体   English

选择每个月的最后一天?

[英]Selecting last day of every month?

I have the following data in MySQL:我在 MySQL 中有以下数据:

+------------+-------+
| myDATE     | delta |
+------------+-------+
| 2015-08-29 |    12 |
| 2015-08-30 |    12 |
| 2015-08-31 |    10 |
| 2015-09-01 |     0 |
| 2015-09-02 |  0.15 |
+------------+-------+

I want to run a query that will select the last day of each month and show the data.我想运行一个查询,该查询将选择每个月的最后一天并显示数据。 I thought I had it working but its not showing yesterdays data.我以为我可以正常工作,但它没有显示昨天的数据。

select mydate, delta from data group by date_format(mydate, '%Y-%m');

Results in:结果是:

+------------+-------+
| mydate     | delta |
+------------+-------+
| 2015-08-29 |    12 |
| 2015-09-01 |     0 |
+------------+-------+

What am I missing?我错过了什么?

I'm looking for it to return this: (ie. The last day of each month on record)我正在寻找它来返回这个:(即记录的每个月的最后一天)

+------------+-------+
| myDATE     | delta |
+------------+-------+
| 2015-08-31 |    10 |
| 2015-09-02 |  0.15 |
+------------+-------+
select last_day(mydate), sum(delta) from data group by last_day(mydate)

last_day is a function which returns the last day of the month the given date last_day 是一个函数,它返回给定日期的月份的最后一天

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day

to answer the updated question回答更新的问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM