简体   繁体   English

在MySql中按第一个查询分组

[英]Group By First Query in MySql

I need to convert the following (sql in access) to mysql. 我需要将以下(访问中的sql)转换为mysql。 It is a group by first query, but obv mysql can't handle first or last. 它是第一个查询的组,但是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))

Basically I am after the time at which the game (hometeam v awayteam) goes in play (runningstatus = 1) 基本上我是在比赛(hometeam v awayteam)进场的时间之后(runningstatus = 1)

Thanks in advance 提前致谢

Not sure what the date is for, but what about using the regular aggregates? 不知道日期是什么,但使用常规聚合怎么样?

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

I assume that the purpose of first is to get the earliest date. 我认为first的目的是获得最早的日期。 If there is at most one row per date, then you can do: 如果每个日期最多有一行,那么您可以:

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;

I moved the condition on RunningStatus to the where clause, because that is more efficient. 我将RunningStatus上的条件移动到where子句,因为这样更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM