mysql - 如何让 MySQL 5.5 中的工作查询在 MySQL 5.7 中工作?

标签 mysql subquery sql-order-by mysql-5.5 mysql-5.7

我花了很多很多时间试图解决这个问题。所以我的查询在不同的 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/

相关文章:

mysql - 使用sql子查询更新mysql数据库

MySQL 子查询/连接

MYSQL 按列总和排序

mysql - 删除后更新排序键

android - 从在线数据库检索今天的数据

c# - 数据库插入性能

php - MYSQL - 计算一个角度是否在两个角度之间

mysql - 尝试编写一个查询来获取最高和最低收入并打印帐户健康状况?

MySQL更新前触发

mysql - MySQL 中按 VARCHAR 值排序