我有一个表,我想返回所有记录的名字和父亲的名字与最新的数据,应该是动态的,每当一个新的最新日期被插入返回该记录。我试过了
SELECT rh.firstname, rh.fathername, rh.bloodonation_date
FROM personprofile rh,
(SELECT MAX(bloodonationdate) AS maxdate, firstname
FROM report_history
GROUP BY firstname) maxresults
WHERE rh.firstname= maxresults.firstname
AND rh.bloodonation_date= maxresults.bloodonation_date;
如果我有这张table
firstname, fathername,bloodonation_date
--------------------------
Ahmed issa 2018-12-24
Noora issa 2018-12-21
Joseph John 2018-12-24
Ash Scott 2018-12-24
Isacf jaack 2018-12-24
Ashley Make 2018-12-24
James Ma 2018-12-20
cd cd 2018-12-24
cde cde 2018-12-24
Marc Mac 2018-12-16
Noora Ahmed 2018-12-15
Rabeew fbdb 2018-11-15
我怎样才能让它回来
firstname, fathername,bloodonation_date
--------------------------
Ahmed issa 2018-12-24
Joseph John 2018-12-24
Ash Scott 2018-12-24
Isacf jaack 2018-12-24
Ashley Make 2018-12-24
cd cd 2018-12-24
2条答案
按热度按时间hmtdttj41#
您可以尝试使用关联子查询。
架构(mysql v5.7)
查询#1
db fiddle视图
2skhul332#
您可以在此处使用子查询:
select*from personprofile where bloodonation\u date=(select max(bloodonation\u date)from personprofile)order by 1,勾选此处的解决方案