簡體   English   中英

在MySql中按第一個查詢分組

[英]Group By First Query in MySql

我需要將以下(訪問中的sql)轉換為mysql。 它是第一個查詢的組,但是obv 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)

提前致謝

不知道日期是什么,但使用常規聚合怎么樣?

SELECT 
    MIN(`Date`) AS `FirstOfDate`, 
    MIN(`Time`) AS `FirstOfTime`, 
    `HomeMatchingId`, 
    `AwayMatchingId`, 
    `RunningStatus`
FROM `20121202`
GROUP BY 
    `HomeMatchingId`, 
    `AwayMatchingId`, 
    `RunningStatus`
HAVING 
    `RunningStatus`=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子句,因為這樣更有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM