Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于MySQL中的两列查找最小值_Mysql_Database_Group By_Min - Fatal编程技术网

基于MySQL中的两列查找最小值

基于MySQL中的两列查找最小值,mysql,database,group-by,min,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 athlet

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

我得到了正确的结果。但我想从一个学科和多个类别中一起选择最佳的表演(例如,男子与少年等)。如何在两列中使用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 
  * 
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 
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
对于每个运动员,一次最佳表现类别。 此结果将与完整的表“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

我试过了,但结果是一样的。

谢谢,我试过这个更新,但是你的查询为每个运动员从每个类别中选择了最佳表现。但是我想选择所有类别的运动员投掷中表现最好的(他个人最好的)。对不起,我不太明白你到底在尝试什么。如果你想要他的个人最佳状态,那么你的问题是正确的。您能否提供一个示例,说明在给定数据的情况下,您的输出应该是什么样的?请再说一件事:)如果结果中有一名运动员两次(运动员在两个不同的类别中有两次相同的表演),我如何编辑SQL查询,使其只进行一次表演?按照编写此查询的方式,运动员只能出现一次(因为嵌套查询中有group by子句)。因此运动员不能出现两次。:)很遗憾,是的,请检查下面的结果。