根据MySQL中的两列查找MIN值

时间:2015-01-26 09:30:21

标签: mysql database group-by min

我在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?

3 个答案:

答案 0 :(得分:0)

也许我不明白,但你可以在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

结果就是你上面描述的那个。

答案 1 :(得分:0)

如果我使用此查询:

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。 这个结果将与全表一起加入"执行"。

答案 2 :(得分:0)

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条款但结果相同。