如何在月底显示数据

时间:2018-12-26 07:53:55

标签: mysql sql date datetime

如何显示特定月份的最新数据?

例如,我有这样的数据

  date      data
2003-04-18 | 12
2003-04-25 | 14
2003-05-16 | 51
2003-05-23 | 21
2003-05-30 | 22
2003-06-06 | 11
2003-06-27 | 14

我要显示数据

  date      data
2003-04-25 | 14
2003-05-30 | 22
2003-06-27 | 14

使用哪种示例查询?

我尝试此查询,但显示所有数据

SELECT tgl_hargaemas, harga FROM tbl_hargaemas
WHERE tgl_hargaemas >= '2003-01-03' - INTERVAL DAYOFMONTH('2018-01-03') MONTH

1 个答案:

答案 0 :(得分:1)

使用简单的GROUP BY子查询查找每个年份月份的最大日期,后跟一个INNER JOIN:

SELECT t.*
FROM (
    SELECT MAX(date) AS maxdate
    FROM t
    GROUP BY YEAR(date), MONTH(date)
) AS g
INNER JOIN t ON g.maxdate = t.date

Demo on DB<>Fiddle