根据 MySQL 中的两列查找 MIN 值
Find MIN value based on two columns in MySQL
我 table 在 MySQL 中进行了运动表演。
结构:
id (PRIMARY KEY)
athlete_id (FOREIGN KEY)
perform
category_id
discipline_id
如果我想 select 从这个 table 中获得最好的表现(每个运动员最多可以进入一次结果),我使用这个查询:
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 将一个学科和更多类别的最佳表现结合在一起(例如,男子与青少年等)。如何对两列使用 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
每个 athlete_id 一次表现最好 category_id。
此结果将与完整的 table "perform".
JOIN
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 但结果是一样的。
我 table 在 MySQL 中进行了运动表演。
结构:
id (PRIMARY KEY)
athlete_id (FOREIGN KEY)
perform
category_id
discipline_id
如果我想 select 从这个 table 中获得最好的表现(每个运动员最多可以进入一次结果),我使用这个查询:
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 将一个学科和更多类别的最佳表现结合在一起(例如,男子与青少年等)。如何对两列使用 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
每个 athlete_id 一次表现最好 category_id。 此结果将与完整的 table "perform".
JOINSQL:
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 但结果是一样的。