sql,加入2个表

时间:2014-09-15 21:01:37

标签: php sql sqlite join

我有2张桌子

matchdata: id_team1,id_team2,name_team1,name_team2,group_order_id

队: team_id,team_name

我试试这个:

select * from matchdata join teams on matchdata.id_team1 = teams.team_id;

但我需要

matchdata.id_team1 = teams.team_id

matchdata.id_team2 = teams.team_id

之后我想要select WHERE group_order_id = $bla 我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您可以在一张桌子上使用两个JOIN

SELECT * FROM matchdata
JOIN teams t1 ON matchdata.id_team1 = t1.team_id
JOIN teams t2 ON matchdata.id_team1 = t2.team_id
WHERE group_order_id = $bla;

答案 1 :(得分:0)

您可以使用IN

SELECT *
FROM matchdata md 
    JOIN teams t ON t.team_id IN (md.id_team1, md.id_team2)
WHERE md.group_order_id = $bla