我有两个父表,它们在子表中创建一个复合表(结果)
我想获取每位运动员的最新记录(仅1条)
我知道答案是按照选择最大日期的比赛并仅以这种方式显示的,但是我一直在尝试这样做,但是我无法正确设置格式
父表(运动员)+---------+-------+| athlete | name |+---------+-------+| 1 | James || 2 | Gemma |+---------+-------+
父表(竞赛)+-----+----------+----------+| cid | compname | compdate |+-----+----------+----------+| 1 | Comp A | 2015 || 2 | Comp B | 2014 |+-----+----------+----------+
子表(结果)+---------+-----+--------+| athlete | cid | result |+---------+-----+--------+| 1 | 1 | 500 || 1 | 2 | 550 || 2 | 2 | 450 |+---------+-----+--------+
预期结果:+-------+----------+--------+| name | compname | result |+-------+----------+--------+| James | Comp A | 500 || Gemma | Comp B | 450 |+-------+----------+--------+
任何想法?谢谢!
最佳答案
我相信这会收到您追求的结果。
SELECT a.`name`, b.`compname`, c.`result`
FROM `compresults` c
INNER JOIN `competitions` b ON b.cid = c.cid
INNER JOIN `athletes` a ON c.`athlete` = a.`athlete`
WHERE b.`compdate` = (
SELECT co.`compdate`
FROM `competitions` co INNER JOIN `compresults` cr ON cr.`cid` = co.`cid`
WHERE cr.`athlete` = a.`athlete`
ORDER BY co.`compdate` DESC LIMIT 1
)