繁体   English   中英

根据MySQL中的两列查找MIN值

[英]Find MIN value based on two columns in MySQL

我在MySQL的比赛中表现出色。

结构体:

id (PRIMARY KEY)    
athlete_id (FOREIGN KEY)    
perform     
category_id     
discipline_id

如果我想从该表中选择最佳表现(每个运动员最多可以出现一次结果),请使用以下查询:

SELECT 
  * 
FROM
  performs NATURAL 
  JOIN athletes 
  JOIN 
    (SELECT 
      athlete_id,
      MIN(perform) AS perform,
      category_id,
      discipline_id 
    FROM
      zaznamy 
    WHERE discipline_id = 4 
      AND category_id = 3 
    GROUP BY athlete_id) rec 
    ON performs.athlete_id = rec.athlete_id 
    AND performs.perform = rec.perform 
    AND performs.category_id = rec.category_id 
    AND performs.discipline_id = rec.discipline_id 
ORDER BY performs.perform 
LIMIT 25 

我得到正确的结果。 但是我想从一个学科和更多类别中选择最佳的表现(例如,男子与少年等)。 如何在两列中使用GROUP BY子句?

也许我听不懂,但是可以像下面的示例一样在group by中添加两列。 只需在两列或更多列之间添加,(逗号)。

SELECT 
* 
FROM
  performs NATURAL 
  JOIN athletes 
  JOIN 
    (SELECT 
      athlete_id,
      MIN(perform) AS perform,
      category_id,
      discipline_id 
    FROM
      zaznamy 
    WHERE discipline_id = 4 
    GROUP BY athlete_id,category_id) rec 
    ON performs.athlete_id = rec.athlete_id 
    AND performs.perform = rec.perform 
    AND performs.category_id = rec.category_id 
    AND performs.discipline_id = rec.discipline_id 
ORDER BY performs.perform 
LIMIT 25 enter code here

编辑-更新

好的,现在我了解您的问题了。 实际上,这很普遍,您首先要查找组的最大值,然后再请求该值的其他信息。

实现此目的的一种方法是使用嵌套查询,如下所示

SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
inner join
(SELECT
    athlete_id, MIN(perform) AS perform
FROM 
    performs
WHERE
    discipline_id = 4 AND category_id IN (1,3,5,7,9) 
GROUP BY
    athlete_id) tt
    on tt.athlete_id = ss.athlete_id and ss.perform = tt.perform

结果就是您上面描述的结果。

如果我使用此查询:

SELECT
    athlete_id, MIN(perform) AS perform, category_id 
FROM 
    performs
WHERE
    discipline_id = 4 AND category_id IN (1,3,5,7,9) 
GROUP BY
    athlete_id, category_id

我得到以下结果:

athlete_id perform category_id
1          11,14      1
1          11,54      3
1          11,54      5
1          10,71      7
2          11,04      1
2          11,24      3
2          11,54      5
2          12,14      7
3          10,94      1
4          10,94      1
4          11,34      3

但是我需要这个结果:

athlete_id perform category_id
1          10,71      7
2          11,04      1
3          10,94      1
4          10,94      1

对于每个运动者ID,最好立即执行category_id。 该结果将与完整表“ perform”一起加入。

SQL:

SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
INNER JOIN
(SELECT
 athlete_id, MIN(perform) AS perform
FROM 
 performs
WHERE
 discipline_id = 4 AND category_id IN (1,3,5,7,9) 
GROUP BY
 athlete_id) tt
ON tt.athlete_id = ss.athlete_id and ss.perform = tt.perform

结果:

athlete_id    perform    category_id
1             7,04       1
2             7,14       1
3             7,14       1
4             7,24       1
5             7,14       1
6             7,24       1
6             7,24       3
7             7,02       1
8             7,07       1
9             7,34       1
10            7,34       1
11            7,18       1
12            7,04       3
13            7,44       1
13            7,44       5
14            7,44       1
15            7,27       1

我尝试过DISTINCT子句,但结果相同。

暂无
暂无

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

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