2012-05-20 92 views
1

我似乎正在努力與這一個,我希望有人能爲我散光。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 

回答

1

創建一個視圖和

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 

更新的查詢左連接其他表吧。

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

這工作很好。你忘了FROM,但沒什麼大不了的。謝謝! – dbinott

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 

我加入了獲取最新活動的子查詢...

+0

我用你的查詢,但我從petevent.startdate和petevent.id返回NULL,但該行確實存在。 已更新後的查詢 – dbinott

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 
+0

我沒有使用MS SQL。 AFAIK沒有Rank()和以上。 – dbinott