MySQL Command Joining -


i store list of events in db , list of teams in same db different table.

info_events

eventid | starttime | hometeam | awayteam

info_teams

teamid | teamname

in info_events table, hometeam , awayteam both foreign keys teamid table.

  select        info_events.eventid,         info_events.eventdate,        info_events.hometeam,        info_events.awayteam,        info_team.teamname            info_events      inner join        info_teams      on        info_events.hometeam = info_teams.teamid 

this current command , works, getting hometeam. how away team name in same command?

you need join info_events table twice, requires unique alias name tables.

select ie.eventid, ie.starttime, t1.teamname hometeam, t2.teamname awayteam info_events ie join info_teams t1 on t1.teamid = ie.hometeam join info_teams t2 on t2.teamid = ie.awayteam 

if of events may not have data info_teams may use left join instead of inner join


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -