表matches
具有以下列:
id, teamA, teamB
表teams
包含:
id, name
我想输出两个团队,并设法做到这一点:
$sql1 = SELECT teams.name FROM matches LEFT JOIN teams ON matches.teamA = teams.id
$sql2 = SELECT teams.name FROM matches LEFT JOIN teams ON matches.teamb = teams.id
然后我用一个while循环输出它:
echo $sql1->name, $sql2->name
我在尝试一个查询时遇到的问题是,每行只能输出一次团队名称。
这将如何与一个查询一起使用?
SELECT team1.name as team1name, // Select the first team name from the table aliased as team1, aliasing that column as team1
team2.name as team2name // Select the second team name from the table aliased as team2, aliasing that column as team2
FROM matches
LEFT JOIN teams team1 // Alias our first join to teams on teamA as team1
ON matches.teamA = team1.id
LEFT JOIN teams team2 // Alias our second join to teams on teamB as team2
ON matches.teamB = teams2.id
执行此查询时,您应该返回 2 个团队名称,一个别名为team1name
,另一个别名为team2name
;因此,当您将这些检索到 PHP 时,它们将具有这些别名列名
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句