繁体   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