如果我有一张表:
FixtureID, HomeTeam, AwayTeam
我可以在浏览器中显示与HomeTeam和AwayTeam相关的ID吗? HomeTeam和AwayTeam都与Teams表中的TeamID相关。
我想显示所有灯具,然后将“TeamID”替换为“TeamName”,以便显示名称而不是ID?
到目前为止,我有:
$sql = <<<SQL
SELECT fix.*, tea.*
FROM Fixtures fix
INNER JOIN Teams tea USING (TeamID)
SQL;
然后
echo '<p>Fixtures</p>';
echo '<div>'.$row['HomeTeam'].' v '.$row['AwayTeam'].'</div>';
编辑:
好的,所以我找到了一个类似于我需要的帖子,并尝试了以下内容:
$sql = <<<SQL
SELECT fix.*, tea1.*, tea2.*
FROM Fixtures fix
INNER JOIN Teams tea1 ON fix.HomeTeam = tea1.TeamID
INNER JOIN Teams tea2 ON fix.AwayTeam = tea2.TeamID
SQL;
然而,它仍然只显示团队ID而不是团队表中的名称。
答案 0 :(得分:0)
自己解决了
$sql = <<<SQL
SELECT f.*, t1.TeamName 'HomeTeam', t2.TeamName 'AwayTeam'
FROM Fixtures f
INNER JOIN Teams t1 ON f.HomeTeam = t1.TeamID
INNER JOIN Teams t2 ON f.AwayTeam = t2.TeamID
SQL;