简体   繁体   English

当聚合函数相等时,MySQL Group返回多个结果

[英]MySQL Group returns multiple results when aggregate function is equal

I am trying to obtain a list of results from a database table ('result'), however, each member may have submitted multiple results, and I wish to retrieve their best time. 我正在尝试从数据库表中获取结果列表(“结果”),但是,每个成员可能都提交了多个结果,因此我希望获得他们的最佳时间。

The query below works (although I'm not sure if it is efficient), except in the case where there are multiple results from the same competitor with the same time scored. 下面的查询有效(尽管我不确定它是否有效),除非同一竞争对手在同一时间获得了多个结果。 (eg competitor #2 scores time 32.15 seconds twice). (例如,竞争对手2得分两次32.15秒)。 In this situation I get two rows for this competitor, rather than the one I'd like. 在这种情况下,我为此竞争对手得到两行,而不是我想要的那一行。

SELECT result_id FROM ( 
    SELECT member_id,MIN(time) AS mintime FROM result 
        JOIN member_result ON result.id = member_result.result_id 
        WHERE event_id = ? 
        GROUP BY member_id ORDER BY mintime ) AS x 
JOIN result ON result.time = x.mintime
JOIN member_result ON member_result.result_id = result.id
AND member_result.member_id = x.member_id

Any suggestions to fix the issue would be greatly received. 任何解决该问题的建议都会收到。

EDIT: Structure of tables as requested; 编辑:表结构按要求;

member_result (TABLE)
 id int unsigned (10)
 result_id int unsigned (10)
 member_id int unsigned (10)
 club_id int unsigned (10)

result (TABLE)
 id int unsigned (10)
 time decimal (6)
 is50mPool BIT ()
 date date ()
 verified BIT ()
 verified_date timestamp ()
 verified_comment varchar (255)
 hasEmailed BIT ()
 enabled BIT ()
 event_id int unsigned (10

sample issue: result: id#1 for event#2, time 60 member_result: id#1, result_id 1, member_id 3 result: id#2 for event#2, time 60 member_result: id#2, result_id 2, member_id 3 样本问题:结果:事件#2的ID#1,时间60成员_结果:ID#1,result_id 1,成员ID 3,结果:事件#2的ID#2,时间60成员_结果:ID#2,结果ID,2,成员ID 3

I'd just like one row returned, with either result_id - currently both rows are returned as the min time is equal. 我只想返回一行,其中任何一个result_id-当前的两行都是在最小时间相等的情况下返回的。

EDITED: 编辑:

You can try to use MIN or MAX depending on which is more intresting you on the ID, and then there is only one row: 您可以尝试使用MIN或MAX,具体取决于哪一个更吸引您使用ID,然后只有一行:

like: 喜欢:

SELECT result_id FROM ( 
    SELECT member_id, MIN(member_result.id) as member_resultID ,MIN(time) AS mintime FROM result 
        JOIN member_result ON result.id = member_result.result_id 
        WHERE event_id = 2 
        GROUP BY member_id ORDER BY mintime ) 
AS x 
JOIN result ON result.time = x.mintime
JOIN member_result ON member_result.result_id = result.id
AND member_result.id = x.member_resultID

This returns only the MIN time for the given event for the first user. 对于第一个用户,这仅返回给定事件的MIN时间。 (so only one row instead of two) (因此只有一行而不是两行)

and here is an SqlFiddle for this. 这是一个SqlFiddle

SELECT Distinct result_id FROM ( 
SELECT member_id,MIN(time) AS mintime FROM result 
    JOIN member_result ON result.id = member_result.result_id 
    WHERE event_id = ? 
    GROUP BY member_id ) AS x 
JOIN result ON result.time = x.mintime
JOIN member_result ON member_result.result_id = result.id
AND member_result.member_id = x.member_id 

have you tried this? 你尝试过这个吗?

Actually, this turned out to be as simple as grouping again on the outer query - something which didn't appear to work initially because of a LIMIT statement I had in place without an ORDER BY before the limit that because of the GROUP BY the query broke. 实际上,这就像在外部查询上再次进行分组一样简单-最初由于在没有ORDER BY的情况下我就已经有了LIMIT语句,而由于基于GROUP BY的查询,在没有限制的情况下,最初似乎没有任何作用坏了。

SELECT result_id FROM ( 
SELECT member_id,MIN(time) AS mintime FROM result 
    JOIN member_result ON result.id = member_result.result_id 
    WHERE event_id = ? 
    GROUP BY member_id ORDER BY mintime ) AS x 
JOIN result ON result.time = x.mintime
JOIN member_result ON member_result.result_id = result.id
AND member_result.member_id = x.member_id
GROUP BY member_result.member_id

Thanks to all those who contributed! 感谢所有贡献者!

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

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