How to join tables together - SQL

后端 未结 2 1264
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 21:50

This is a SQL question

I have two tables I need to join together, using the inner join syntax. One named Entry, and the other named prize. I need to list the event

相关标签:
2条回答
  • 2021-01-23 22:38

    You should use alias to avoid ambiguity

    select 
    e.event_id,
    e.horse_id,
    e.place,
    p.Money
    from ENTRY e 
    join PRIZE p on p.event_id = e.event_id
    
    0 讨论(0)
  • SELECT e.Event_id, e.Horse_id, e.place, p.money
    
    FROM ENTRY e join Prize p
    
    ON e.Event_id = p.Event_id
    
    where e.place = p.place;
    

    The e and p are used as aliases for the tables to avoid unreadable sql because of long table names.

    using the e. or p. you will select the field for that table because it is possible that both tables have a field with the same name so there will be issues when executing the statement

    I added the e.place = p.place because if you don't you would be getting the results for every place for each event matched with every prize

    for example you would get Event 1 horse 1 place 1 prize 1 event 1 horse 1 place 1 prize 2 Event 1 horse 1 place 1 prize 3 event 1 horse 1 place 1 prize 4 etc... until you get every prize and this would be the same for every entry, assuming the event for the prize equals the event for the entry

    0 讨论(0)
提交回复
热议问题