2014-01-22 18 views
0

我需要將以下內容(sql in access)轉換爲mysql。它是第一個查詢的組,但obv mysql不能處理第一個或最後一個。通過MySql中的第一個查詢分組

SELECT First([20121202].Date) AS FirstOfDate, First([20121202].Time) AS FirstOfTime, [20121202].HomeMatchingId, [20121202].AwayMatchingId, [20121202].RunningStatus 
FROM 20121202 
GROUP BY [20121202].HomeMatchingId, [20121202].AwayMatchingId, [20121202].RunningStatus 
HAVING ((([20121202].RunningStatus)=1)) 

基本上我在這遊戲(hometeam v awayteam)變爲重放時後(runningstatus = 1)

在此先感謝

+0

請提供架構,示例數據和所需輸出。 – RedFilter

回答

1

不知道的日期是什麼,但那麼使用常規聚合體呢?

SELECT 
    MIN(`Date`) AS `FirstOfDate`, 
    MIN(`Time`) AS `FirstOfTime`, 
    `HomeMatchingId`, 
    `AwayMatchingId`, 
    `RunningStatus` 
FROM `20121202` 
GROUP BY 
    `HomeMatchingId`, 
    `AwayMatchingId`, 
    `RunningStatus` 
HAVING 
    `RunningStatus`=1 
+0

爲什麼我使它複雜....?大。謝謝。根本不需要日期。 – John

+0

我該如何添加homematchingid不等於零的條件?我已經嘗試添加到有聲明,但它'沒有'它沒有它 – John

+0

或事實上日期和時間不等於零 – John

1

我認爲first的目的是爲了得到最早的日期。如果在每個日期最一行,那麼你可以做:

SELECT t.Date AS FirstOfDate, t.Time AS FirstOfTime, 
     t.HomeMatchingId, t.AwayMatchingId, t.RunningStatus 
FROM `20121202` t join 
     (select HomeMatchingId, AwayMatchingId, min(date) as mindate 
     from `20121202` 
     WHERE RunningStatus = 1; 
     group by HomeMatchingId, AwayMatchingId 
    ) tmin 
     on t.date = tmin.date and t.HomeMatchingId = tmin.HomeMatchingId and 
     tmin.AwayMatchingId and t.AwayMatchingId and 
WHERE RunningStatus = 1; 

我提出RunningStatus條件的where條款,因爲這是更有效的。