MySQL多个连接和最大日期

时间:2012-05-20 16:22:56

标签: mysql sql

我似乎正在努力解决这个问题,我希望有人能为我带来一丝光明。

petevents将有多个重复事件,但日期不同。我只想要每个petevent中最新的一个。

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

3 个答案:

答案 0 :(得分:2)

如果您有重复的活动,您必须获得最新的活动,请尝试以这种方式:

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
(SELECT * FROM petevents p1 WHERE p1.startdate = 
(SELECT MAX(startdate) FROM petevents p2 WHERE p1.petid = p2.petid) ) 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

基本上修改后的部分就是这个:

LEFT OUTER JOIN
    (SELECT * FROM petevents p1 WHERE p1.startdate = 
    (SELECT MAX(startdate) FROM petevents p2 WHERE p1..petid = p2.petid) ) ON pets.id = petevents.petid and petevents.eventid = petroutineschedules.eventid

我加入了一个获得最新活动的子查询......

答案 1 :(得分:1)

创建一个视图并使用它连接其他表。

CREATE VIEW  PETEVNT_VW AS SELECT PETID,EVENTID, MAX(STARTDATE) AS MAXSTARTDATE GROUP BY PETID,EVENTID

答案 2 :(得分:0)

Select 
name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate
from (
SELECT 
name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate,
Rank() over (Partition BY petevents.startdate order by petevents.startdate DESC) as Rank
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
)
where rank = 1