mysql找最大日期的数据

1.在一个数据表里,如果statistic_date为主键,其他数据非主键,则找最大日期的数据为:

select * from table_1 where statistic_date=(SELECT max(statistic_date) from table_1)

2.在一个数据表里,如果fund_id、statistic_date为主键,其他数据非主键,则分别找每条fund_id最大日期的数据为:

SELECT fdre.* FROM
(SELECT fund_id,MAX(statistic_date) msd FROM `table_2`
GROUP BY fund_id) T
INNER JOIN table_2  fdre
ON fdre.fund_id = T.fund_id
AND fdre.statistic_date = T.msd

3.在一个数据表里,如果fund_id、year、statistic_date为主键,其他数据非主键,则分别找每条fund_id,每个年最大日期的数据为:

SELECT fdre.* FROM
(SELECT fund_id,`year`,MAX(statistic_date) msd FROM `table_2`
GROUP BY fund_id,year) T
INNER JOIN table_2  fdre
ON fdre.fund_id = T.fund_id
AND fdre.statistic_date = T.msd
AND fdre.year= T.year

猜你喜欢

转载自blog.csdn.net/qq_44821149/article/details/130934259
今日推荐