简体   繁体   English

SQL根据相关表值获取所有n:th行

[英]Sql get all n:th rows depending on related table value

Hello I'm doing a sport statistics site, which provides alot of challenges when it comes to the sql, but before this point I have been able to solve all the queries getting the collected stats I want. 您好,我正在做一个体育统计网站,它涉及到很多有关sql的挑战,但是在此之前,我已经能够解决所有查询,以获取所需的统计数据。 However now I find myself asking for some help. 但是现在我发现自己在寻求帮助。

I'm using a mysql database. 我正在使用mysql数据库。

It has a games table, with all games and it has a gameevents table with all gameevents, like goals and penalties. 它具有一个包含所有游戏的游戏表,并且具有一个包含所有游戏事件(例如目标和惩罚)的gameevents表。

Now I want to collect all the game winning goals. 现在,我想收集所有的比赛获胜目标。 Which means i want to get all games where our score is bigger than opposing team score, and the get the goal that means that our team comes out one goal ahead. 这意味着我想让所有比赛的得分都大于对战球队的得分,并且获得进球意味着我们的球队领先一个目标。 So if ourscore is 6 and theirscore (opposing team) is 2 i want to collect the third goal that our team made (gameevent = 1, thisteam = 1) from gameevents table 因此,如果我们的得分是6,而他们的得分(反对的团队)是2,我想从gameevents表中收集我们的团队制定的第三个目标(gameevent = 1,thisteam = 1)

table games:
id
ourscore
theirscore

table gameevents:
id
game_id
player_id
eventtype
thisteam
time

Below query would get all goal rows that our team made in games which we won 下面的查询将获取我们团队在赢得比赛的比赛中做出的所有目标行

select ge.* from gameevents ge
inner join games g
on ge.game_id = g.id
where g.ourscore > g.theirscore
and ge.eventtype = 1
and ge.thisteam = 1

Below query shows a bit of what I want to do now, however the Limit clause will not take any parameters. 下面的查询显示了我现在要执行的操作,但是Limit子句将不包含任何参数。

select ge.* from gameevents ge
where ge.id = (
    select ge2.id 
    from gameevents ge2
    inner join games g
    on ge2.game_id = g.id
    where g.ourscore > g.theirscore
    and ge2.eventtype = 1
    and ge2.thisteam = 1
    order by ge2.time
    limit g.theirscore + 1, 1
)

So if our team has won three games with 2-1, 4-1 and 6-3 i want to get the row for the second goal in game one, the second goal on game two and the fourth goal in game three. 因此,如果我们的球队以2-1、4-1和6-3赢得了三场比赛,我想获得第一场第二个进球,第二场第二个进球和第三场第四个进球的排名。

Any ideas to have me moving forward? 有什么想法让我前进吗?

I wish to solve it without declaring variables and stuff, just a plain old school query. 我希望在不声明变量和东西的情况下解决它,而只是简单地在老式学校查询。

edit: 编辑:

Feels like I'm so close. 感觉我很亲近。 Have tried to use some variables to make it possible since i saw a possible solution in row_number() but that was only in mssql. 尝试使用一些变量使之成为可能,因为我在row_number()中看到了可能的解决方案,但这仅在mssql中。

This link pointed me in this direction: http://www.explodybits.com/2011/11/mysql-row-number/ 该链接向我指出了这个方向: http : //www.explodybits.com/2011/11/mysql-row-number/

This is where I am now: 这是我现在的位置:

SELECT subquery.id, game_id, player_id, eventtype, time
FROM
(
    SELECT  @row_num := IF(@prev_value=ge.game_id,@row_num+1,1) AS RowNumber
    ,ge.id
    ,ge.game_id
    ,ge.player_id
    ,ge.eventtype
    ,ge.time
    ,RowNumber
    ,@prev_value := ge.game_id
    FROM gameevents AS ge
    INNER JOIN games AS g
    ON ge.game_id = g.id,
        (SELECT @row_num := 1) x,
        (SELECT @prev_value := '') y
    WHERE g.ourscore > g.theirscore
    AND ge.eventtype = 1
    AND ge.thisteam = 1
    ORDER BY ge.game_id, ge.time
    ) subquery
INNER JOIN games g2
ON g2.id = subquery.game_id
WHERE Rownumber = g2.theirscore + 1

This almost works, the count switches back to 1 when there is a new game_id, but on some games the count start on 9 then move down to 1, instead of starting on 1 and counting upwards. 这几乎可行,当有新的game_id时,计数切换回1,但是在某些游戏上,计数从9开始,然后向下移动至1,而不是从1开始并向上计数。

edit: 编辑:

solution: 解:

Actually I came up with a solution to this when I was looking through my old bookmarked questions here and here it is (with help from this link: http://www.artfulsoftware.com/infotree/qrytip.php?id=1098 ): 实际上,当我在这里和这里浏览我的旧书签问题时,我想出了一个解决方案(在此链接的帮助下: http : //www.artfulsoftware.com/infotree/qrytip.php?id=1098 ) :

SET @gameprev=0, @gameprev2=0;

SELECT goals.player_id FROM (

SELECT game_id, player_id, time, row_number
FROM 
(
    SELECT player_id, time, @gameprev2 := if(@gameprev = game_id, @gameprev2+1, 1) AS row_number, @gameprev := game_id AS game_id
    FROM gameevents
    WHERE eventtype = 1
    AND thisteam = 1
    ORDER BY game_id, time
    ) as goalorder
) as goals
INNER JOIN games AS g
ON goals.game_id = g.id
WHERE g.ourscore > g.theirscore
AND goals.row_number = g.theirscore +1

Seems MySQL LIMIT doesn't allow dynamic values. 似乎MySQL LIMIT不允许动态值。

See here. 看这里。

How to write a SQL query with dynamic LIMIT 如何使用动态LIMIT编写SQL查询

Not sure if you can do it in one query and I don't see why you want to do it so. 不知道您是否可以在一个查询中做到这一点,我不知道您为什么要这样做。 I would first try to load in some table the ordinals which I want and the respective game IDs - say 2 /game 1/, 2 /game 4/, 4 /game 9/. 我首先尝试在一张桌子上加载我想要的序号和相应的游戏ID,例如2 / game 1 /,2 / game 4 /,4 / game 9 /。

Then I would use some technique similar to what is mentioned here on the above page. 然后,我将使用类似于上一页在此处提到的技术。

So I am not sure if you can do it with one old-school query as you call it. 因此,我不确定是否可以用一个老式查询来做到这一点。 You might need a loop, or temp table, or something like that. 您可能需要一个循环,临时表或类似的东西。 I am curious myself to figure out how it can be done exactly in the simplest possible way. 我很想知道如何才能以最简单的方式准确地做到这一点。

Just giving you some directions, hope it helps. 只是给您一些指示,希望对您有所帮助。

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

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