简体   繁体   English

内部联接两个表,仅显示最新记录(mySQL)

[英]Inner Join two tables and make only latest record appear (mySQL)

I have two parent tables which create a composite inside the child table (compresults) 我有两个父表,它们在子表中创建一个复合表(结果)

I want to get the most recent record (only 1) for each athlete 我想获取每位运动员的最新记录(仅1条)

I know the answer is along the lines of selecting the competition with the max date and only displaying it that way, however i have been trying to do this but i cannot get the formatting right 我知道答案是按照选择最大日期的比赛并仅以这种方式显示的,但是我一直在尝试这样做,但是我无法正确设置格式

Parent Table (athletes) +---------+-------+ | athlete | name | +---------+-------+ | 1 | James | | 2 | Gemma | +---------+-------+ 父表(运动员) +---------+-------+ | athlete | name | +---------+-------+ | 1 | James | | 2 | Gemma | +---------+-------+ +---------+-------+ | athlete | name | +---------+-------+ | 1 | James | | 2 | Gemma | +---------+-------+

Parent Table (competitions) +-----+----------+----------+ | cid | compname | compdate | +-----+----------+----------+ | 1 | Comp A | 2015 | | 2 | Comp B | 2014 | +-----+----------+----------+ 父表(竞赛) +-----+----------+----------+ | cid | compname | compdate | +-----+----------+----------+ | 1 | Comp A | 2015 | | 2 | Comp B | 2014 | +-----+----------+----------+ +-----+----------+----------+ | cid | compname | compdate | +-----+----------+----------+ | 1 | Comp A | 2015 | | 2 | Comp B | 2014 | +-----+----------+----------+

Child Table (compresults) +---------+-----+--------+ | athlete | cid | result | +---------+-----+--------+ | 1 | 1 | 500 | | 1 | 2 | 550 | | 2 | 2 | 450 | +---------+-----+--------+ 子表(结果) +---------+-----+--------+ | athlete | cid | result | +---------+-----+--------+ | 1 | 1 | 500 | | 1 | 2 | 550 | | 2 | 2 | 450 | +---------+-----+--------+ +---------+-----+--------+ | athlete | cid | result | +---------+-----+--------+ | 1 | 1 | 500 | | 1 | 2 | 550 | | 2 | 2 | 450 | +---------+-----+--------+

EXPECTED RESULT: +-------+----------+--------+ | name | compname | result | +-------+----------+--------+ | James | Comp A | 500 | | Gemma | Comp B | 450 | +-------+----------+--------+ 预期结果: +-------+----------+--------+ | name | compname | result | +-------+----------+--------+ | James | Comp A | 500 | | Gemma | Comp B | 450 | +-------+----------+--------+ +-------+----------+--------+ | name | compname | result | +-------+----------+--------+ | James | Comp A | 500 | | Gemma | Comp B | 450 | +-------+----------+--------+

Any Idea? 任何想法? Thanks! 谢谢!

try using 尝试使用

select b.name,c.compname,a.result from compresults a left join athletes 
b on (a.athlete)=b.athlete
left join competitions c on (a.cid)=c.cid

I believe this receives the result you're after. 我相信这会收到您追求的结果。

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
                    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM