如何从两个SQL表中进行选择

时间:2015-04-11 05:35:47

标签: mysql

我正在尝试使用外键链接我的SQL表,然后使用select语句调用数据。我的表是

team table
team_id 
name

match table
match_id
team1_id
team2_id
round

我想从匹配表中选择match_id,其中team1的名称是" alpha" team2的名字是" bravo"和圆是1.认为我需要一个JOIN,但有点坚持如何实现它。

2 个答案:

答案 0 :(得分:1)

Try this:

select m.* 
from team  t inner join on match m on t.team_id = m.team1_id and t.team2_id 
where t.name in ('alpha','bravo') and m.round = 1

答案 1 :(得分:0)

试试这个:

SELECT mt.*, tbl1.name as team1_name,tbl2.name as team2_name  from match_table as mt 
JOIN teamtable as tbl1 ON mt.team_id=tbl1.team1_id 
JOIN teamtable as tbl2 ON mt.team_id=tbl2.team12_id
WHERE mt.team1_id='alpha' AND mt.team2_id='bravo' AND mt.round=1;

未经测试。测试一下。