我有两张桌子(比赛,球队)。在匹配表中我有id,team1,team2(其中team1和team2是团队的ID)。在团队表中我有id和名称..如何在php中打印匹配但显示团队名称而不是他们的ID?
我的代码(仅打印teamid vs teamid):
<?php
require_once 'conf/conf.php';
require_once 'functions.php';
$query = mysql_query("SELECT time1 as t1id, time2 as t2id
FROM matches
INNER JOIN teams
ON
teams.id = matches.time1
");
//echo mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$t1 = $row["t1id"];
$t2 = $row["t2id"];
echo ($t1 . " vs. " . $t2 . "<br>");
}
?>
答案 0 :(得分:4)
您可以执行以下操作(假设teams
表格有name
列):
<?php
require_once 'conf/conf.php';
require_once 'functions.php';
$query = mysql_query("SELECT time1 as t1id,
time2 as t2id,
t1.name AS t1name,
t2.name AS t2name
FROM matches
INNER JOIN teams AS t1
ON t1.id = matches.time1
INNER JOIN teams AS t2
ON t2.id = matches.time2");
//echo mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$t1 = $row["t1name"];
$t2 = $row["t2name"];
echo ($t1 . " vs. " . $t2 . "<br>");
}
?>
答案 1 :(得分:2)
试试这个
require_once 'conf/conf.php';
require_once 'functions.php';
$query = mysql_query("SELECT teams1.id as t1id,teams1.name as name1, teams2.id as t2id, teams2.name as name2
FROM matches
INNER JOIN teams as teams1 ON teams1.id = matches.time1
INNER JOIN teams as teams2 ON teams2.id = matches.time2
");
//echo mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$t1 = $row["name1"];
$t2 = $row["name2"];
echo ($t1 . " vs. " . $t2 . "<br>");
}
答案 2 :(得分:0)
正如我在对你的问题的评论中所说,加入桌子两次:</ p>
SELECT t1.name, t2.name
FROM matches.m
INNER JOIN teams AS t1 ON (t1.id = m.team1)
INNER JOIN teams AS t2 ON (t2.id = m.team2)