私はこれに苦労しているようで、冷たい誰かが私に光を当てることを望んでいました.
petevents には、日付が異なることを除いて、複数の重複したイベントがあります。各ペットイベントの最新のものだけが必要です。
SELECT name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate
FROM pets
LEFT OUTER JOIN petroutineschedules
ON pets.id = petroutineschedules.petid
LEFT OUTER JOIN 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
@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