两个不同表的内部联接语句

时间:2019-07-05 14:29:00

标签: mysql sql

我目前正在尝试内部连接这两个表: Prize table Entry table

到目前为止,我已经提出了这个建议:

SELECT  entry.event_id, entry.horse_id, entry.place, prize.money
FROM entry
INNER JOIN prize
ON entry.place = prize.place
ORDER BY event_id, horse_id;

但这是我正在寻找的预期结果:

Expected Outcome

1 个答案:

答案 0 :(得分:2)

您还必须加入event_id

SELECT entry.event_id, entry.horse_id, entry.place, prize.money
FROM entry
INNER JOIN prize
ON entry.place = prize.place and entry.event_id = prize.event_id
ORDER BY entry.event_id, entry.horse_id;

请参见demo
结果:

> event_id | horse_id | place | money
> -------: | -------: | ----: | ----:
>      101 |      101 |     1 |   120
>      101 |      102 |     2 |    60
>      101 |      201 |     3 |    30
>      102 |      201 |     2 |     5
>      103 |      201 |     3 |    40