我有如下三个表:
1. Results table (containing performances)
2. Athletes table (containing athlete ids and names)
3. Events table (containing event ids and eventNames)
Results Table (sample)
+----------+-----------+---------+----------+----------+------+------------+-----------+--------+----------+---------+-----------------------+--------+----------------+------------+
| resultID | athleteID | eventID | ageGroup | time | wind | distHeight | implement | record | centreID | placing | competition | in_out | venue | date |
+----------+-----------+---------+----------+----------+------+------------+-----------+--------+----------+---------+-----------------------+--------+----------------+------------+
| 203 | 503262 | 1 | MS | 00010.83 | 5.2 | | | D | AKL | 3 | IAAF Challenge | out | Arles FRA | 2008-06-07 |
| 201 | 515092 | 1 | MS | 00011.06 | 3.2 | | | | CAN | 6h1 | Winter Series #3 | out | Gold Coast AUS | 2008-06-15 |
| 298 | 503262 | 1 | MS | 00011.26 | -1.0 | | | D | AKL | 4 | TNT - Fortuna | out | Kladno CZE | 2008-06-18 |
| 202 | 515092 | 2 | MS | 00022.69 | 0.6 | | | | CAN | 4h1 | Winter Series #3 | out | Gold Coast AUS | 2008-06-15 |
| 380 | 504550 | 2 | MS | 00022.22 | 0.9 | | | | AKL | 6rA | Int. Meeting Nivelles | out | Nivelles BEL | 2008-06-28 |
| 381 | 504613 | 2 | MS | 00021.28 | 0.9 | | | | AKL | 2rA | Int. Meeting Nivelles | out | Nivelles BEL | 2008-06-28 |
| 3 | 518131 | 33 | MS | | | 077.91 | 800gm | | WBP | 1Q | Good Luck Beijing | out | Beijing CHN | 2008-05-22 |
| 4 | 518131 | 33 | MS | | | 079.61 | 800gm | | WBP | 1 | Good Luck Beijing | out | Beijing CHN | 2008-05-23 |
| 144 | 518131 | 33 | MS | | | 080.51 | 800gm | | WBP | 1 | Chinese Taipei Int | out | Taiwan TPE | 2008-06-01 |
+----------+-----------+---------+----------+----------+------+------------+-----------+--------+----------+---------+-----------------------+--------+----------------+------------+
Event Table (sample)
+---------+---------------+
| eventID | eventName |
+---------+---------------+
| 1 | 100m |
| 2 | 200m |
| 33 | Javelin Throw |
+---------+---------------+
Athlete Table (sample)
+-----------+----------+-----------+--------+------------+--------+
| athleteID | nameLast | nameFirst | gender | DOB | clubID |
+-----------+----------+-----------+--------+------------+--------+
| 503262 | Lambert | Ben | M | 2003-12-04 | 233 |
| 504613 | Abba | Eric | M | 1991-11-03 | 20 |
| 518131 | Abbiss | Hugh | M | 1992-04-03 | 57 |
+-----------+----------+-----------+--------+------------+--------+
我需要分别返回每个比赛项目的最小(时间)和每个跳跃/ throw 项目的最大(距离)(来自结果表)以及相应的运动员姓名和项目名称。
我已尝试按照 MySql 5.7 的建议在“GROUP BY”列表中包含选择项,但最终返回了数千行。
我有以下在 MySql 5.7 之前有效的查询
SELECT events.eventID, events.eventName, results.ageGroup, results.athleteID, MIN(results.time) AS time, MAX(results.distHeight) as distHeight, DATE_FORMAT(results.date, '%d %b %Y') as date, athletes.nameFirst, athletes.nameLast, DATE_FORMAT(athletes.DOB, '%d %b %Y') as format_DOB
FROM (
SELECT * FROM results
WHERE results.ageGroup = 'MS'
AND YEAR(results.date) = '2019'
AND results.wind < 2.1
AND results.wind != 'nwr'
ORDER BY results.time ASC, results.distHeight DESC, results.date ASC
) as results
INNER JOIN athletes ON results.athleteID = athletes.athleteID
INNER JOIN events ON results.eventID = events.eventID
GROUP BY results.eventID
ORDER BY events.eventID ASC
我希望得到以下结果:
Event | Result | Athlete Name
100m | 10.83 | Ben Lambert ...
200m | 21.28 | Eric Abba ..
Javelin | 80.51m | Hugh Abbiss ...
如有任何帮助或建议,我们将不胜感激......
最佳答案
为了获得更好的性能,您应该将主查询与子查询结合起来
select events.eventID
, events.eventName
, results.ageGroup
, results.athleteID
, t.time
, t.distHeight
, DATE_FORMAT(results.date, '%d %b %Y') as date
, athletes.nameFirst
, athletes.nameLast
, DATE_FORMAT(athletes.DOB, '%d %b %Y') as format_DOB
FROM events
INNER JOIN result ON results.eventID = events.eventID
INNER JOIN athletes ON results.athleteID = athletes.athleteID
INNER JOIN (
SELECT events.eventName
, results.ageGroup
, MIN(results.time) AS time
, MAX(results.distHeight) as distHeight
FROM events
INNER JOIN result ON results.eventID = events.eventID
WHERE results.ageGroup = 'MS'
AND YEAR(results.date) = '2019'
AND results.wind < 2.1
AND results.wind != 'nwr'
GROUP BY events.eventName, results.ageGroup
) t ON t.eventName = event.eventName
AND t.ageGroup = result.ageGroup
AND ( t.time = result.time OR t.distHeight = result.distHeight)
ORDER BY results.time ASC, results.distHeight DESC, results.date ASC
关于mysql - 如何从使用聚合函数分组的多个表中返回多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461539/