我似乎正在努力與這一個,我希望有人能爲我散光。MySQL多個連接和最大日期
petevents將有多個重複事件,除了日期會有所不同。我只想要每個petevent中最新的一個。從@aleroot
SELECT name, pets.id petid, petroutineschedules.id prsid, petroutineschedules.eventid, timeunit, dateunit, petevents.startdate, petevents.id peid
FROM
pets
LEFT OUTER JOIN
petroutineschedules ON pets.id = petroutineschedules.petid
LEFT OUTER JOIN
(
SELECT *
FROM petevents p1
WHERE p1.startdate =
(
SELECT MAX(startdate) FROM petevents p2
WHERE p1.petid = p2.petid
)
) AS petevents
ON pets.id = petevents.petid
AND petevents.eventid = petroutineschedules.eventid
WHERE (pets.id = 1025) AND (pets.deletedat IS NULL)
GROUP BY petevents.startdate , name , pets.id , petroutineschedules.id ,petroutineschedules.eventid , timeunit , dateunit
ORDER BY eventid
這工作很好。你忘了FROM,但沒什麼大不了的。謝謝! – dbinott