一些數據我有這些3個表:獲取對應於最大日期
Table ORG:
Fields:historyid, personid
Table PERSON:
Fields: id
Table HISTORY:
Fields: id,date,personid
歷史和ORG被鏈接到人與一個1:N的關係。此外,ORG以1:N關係鏈接到HISTORY。 我想從表ORG中爲每個人得到一行:這對應於具有最高日期的HISTORY行。以下SQL給出了某個人的最高日期。但是,我不知道如何將這與上述要求結合起來。
SELECT ash1.id
FROM
(SELECT * FROM history a WHERE a.personid=person.id) ash1
LEFT JOIN
(SELECT * FROM history b WHERE b.personid=person.id) ash2
ON ash1.personid=ash2.personid
AND ash1.date < ash2.date
WHERE ash2.date IS NULL
標籤您與您正在使用的數據庫的問題。 –