我花了很多很多时间试图解决这个问题。所以我的查询在不同的 mysql 版本上给出了不同的结果。这导致了我的问题。到目前为止,我发现从 MySQL 5.6 开始,子查询中的 order by 就无法被识别。这似乎是 5.7 中结果错误的原因。
我想我应该以某种不同的方式从“AND EXISTS”开始完成整个结尾,但我不知道如何做。
我已经在 3 个不同的服务器上使用相同的数据库进行了测试。一个为 5.5(有效),两个为 5.7(无效)。
我的查询:有效:5.5,无效:5.6(?)、5.7
SELECT
Sum(P) as games
FROM(SELECT gameday, league, home, season, id, 1 P
FROM games
UNION ALL
SELECT gameday, league, away, season, id, 1
FROM games) as tot, clubs c WHERE tot.home=c.id AND c.id=1 AND
tot.league<=2 AND tot.season=2010
AND EXISTS
(select if(e.event<10,1,0) as first from
(select * from events
where event<=2 or event=9 or event=10 or event=11 or event=13
order by event_time asc) as e
where e.game=tot.id group by e.game having first>0)
有什么想法吗?谢谢。
编辑:
事件可以是 1、2、9、10、11 或 13
我想计算第一个事件 <10 的游戏。游戏的第一个事件由event_time决定。
这有意义吗?
编辑2:
事件表中的列
- id (primary, auto_increment)
- game
- player
- event
- event_time
最佳答案
最终我自己成功了。这是我现在使用的
SELECT
Sum(P) as games
FROM(SELECT gameday, league, home, season, id, 1 P
FROM games
UNION ALL
SELECT gameday, league, away, season, id, 1
FROM games) as tot, clubs c WHERE tot.home=c.id AND c.id=1 AND
tot.league<=2 AND tot.season=2010
AND EXISTS
(select if(e.event<10,1,0) as first from
(select game, min(event_time),
substring_index(group_concat(event order by event_time), ',', 1) as event
from events
where event<=2 or event=9 or event=10 or event=11 or event=13
group by game) as e
where e.game=tot.id having first>0)
关于mysql - 如何让 MySQL 5.5 中的工作查询在 MySQL 5.7 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35881043/