我有匹配表(其中包含" ID,日期,team1_id,team2_id ")
和表团队(其中包含" ID,名称")
我需要显示团队名称的PHP或SQL代码(来自表团队)WHERE "team1_id"
和team2_id" = team.id
......实际上我有类似的东西:
$deleteMatch = Database::query('SELECT match.id, match.team1_id, match.date,
team.id, team.name FROM `match`, `team` WHERE team.id = match.team1_id');
foreach($deleteMatch as $matchinfo)
{
?><option value="<?php echo $matchinfo["id"];?>">
<?php echo $matchinfo["date"]; echo $matchinfo["name"];?></option>
<?php
}
但我需要向foreach添加team2.id(name)
。 (日期| TEAM1 NAME | TEAM2 NAME)
答案 0 :(得分:0)
使用此查询:
SELECT match.id, match.team1_id, match.date,
t1.id as team1ID, t1.name as team1Name,
match.team2_id,t2.id as team2ID, t2.name as team2Name
FROM `match` JOIN `team` t1 ON t1.id = match.team1_id
JOIN `team` t2 ON t2.id = match.team2_id
答案 1 :(得分:0)
$deleteMatch = Database::query('SELECT `match`.id, `match`.team1_id, `match`.date, `team`.id, `team`.name FROM `match` JOIN `team` ON `team`.id = `match`.team1_id' LEFT JOIN `team` ON `team`.id = `match`.team2_id);
foreach($deleteMatch as $matchinfo)
{
?><option value="<?php echo $matchinfo["id"];?>"><?php echo $matchinfo["date"]; echo $matchinfo["name"];?></option>
<?php
}
答案 2 :(得分:0)
我会在团队桌上使用2个内部联接来执行此操作:
$query = "SELECT match.*, team.* FROM match
INNER JOIN team ON match.team1_id = team.id
INNER JOIN team ON match.team2_id = team.id;"
表格看起来像这样:
match.id | match.date | team1_team.name | team2_team.name
答案 3 :(得分:0)
您可以尝试此SQL查询:
SELECT match.id, date, t1.name AS team1, t2.name AS team2
FROM match, team AS t1, team AS t2
WHERE team1_id=t1.id AND team2_id=t2.id
诀窍是你需要将每个团队ID分别与其名称相匹配,因此你必须两次包括团队表。为了区分两个表,您可以在AS
关键字的帮助下使用别名(在示例中,我将别名表分别为t1和t2)
结果表将包含4列:id(匹配ID),日期,team1(团队1的名称)和team2(团队2的名称),因此您可以使用$matchinfo["team1"]
和{{ 1:}在你的PHP代码中。