如何从MySQL 5.5获得工作查询以在MySQL 5.7中工作?

时间:2016-03-09 01:00:09

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

我花了很多时间试图解决这个问题。所以我的查询在不同的mysql版本上给出不同的结果。我的问题。到目前为止,我已经发现,由于子查询中的MySQL 5.6顺序无法识别。这似乎是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确定。

这有意义吗?

EDIT2:

events-table中的列

- id (primary, auto_increment)
- game
- player
- event
- event_time

2 个答案:

答案 0 :(得分:0)

最终让自己工作了。这是我现在使用的

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)

答案 1 :(得分:0)

你的第一个目标是找到每场比赛的第一个合格赛事。 “合格”表示您提到的event值之一。这里有一个很好的伎俩。每个游戏编号最小id的事件是第一个事件。

这样就可以解决这个问题。这是第一个合格活动的ids /游戏列表。

             SELECT MIN(id) id, game
               FROM events
              WHERE event < 2
                 OR event IN (9,10,11,13)
              GROUP BY game

接下来,您需要为每个游戏提供实际的第一个事件,而不仅仅是其ID。

      SELECT a.event, a.game
        FROM event a
        JOIN (
               SELECT MIN(id) id, game
                 FROM events
                GROUP BY game
             ) b ON a.id = b.id
       WHERE a.event < 10

这会产生每个游戏的第一个事件的事件/游戏列表,但它排除了第一个事件值> = 10的游戏。

接下来,您已经正确地提出了主场和客场比赛的联盟列表。此查询为每个游戏生成一行

       SELECT tot.p
         FROM (
               SELECT gameday, league, home, season, id, 1 AS p FROM games
                UNION ALL
               SELECT gameday, league, away, season, id, 1 AS p FROM games
              ) tot
         JOIN clubs c ON tot.home = c.id
        WHERE tot.league <= 2
          AND tot.season >= 2010
          AND c.id = 1

最后,将您选择的游戏加入第一个事件&lt; 10,并将结果加起来。

SELECT SUM(tot.p) gamecount
  FROM (
        SELECT gameday, league, home, season, id, 1 AS p FROM games
         UNION ALL
        SELECT gameday, league, away, season, id, 1 AS p FROM games
       ) tot
  JOIN clubs c ON tot.home = c.id
  JOIN (
          SELECT a.event, a.game
            FROM event a
            JOIN (
                   SELECT MIN(id) id, game
                     FROM events
                    GROUP BY game
                 ) b ON a.id = b.id
           WHERE a.event < 10
       ) eligible ON eligible.game = tot.id
 WHERE tot.league <= 2
   AND tot.season >= 2010
   AND c.id = 1

我写这篇文章的目的是在SQL中显示“S” - 结构化。从简单的构建块开始构建查询可以获得两件事。

  1. 正确答案
  2. 通过运行子查询进行调试的方法,确保它们有效,然后将它们合并到更大的查询中。