Mysql finds the data with the largest date

1. In a data table, if statistic_date is the primary key and other data is not the primary key, then the data to find the maximum date is:

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

2. In a data table, if fund_id and statistic_date are primary keys, and other data are not primary keys, then find the data with the largest date of each fund_id as follows:

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. In a data table, if fund_id, year, and statistic_date are primary keys, and other data are not primary keys, then find each fund_id separately, and the data of the maximum date of each year is:

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

Guess you like

Origin blog.csdn.net/qq_44821149/article/details/130934259