繁体   English   中英

从其他表获取ID名称

[英]Get the names of IDs from different table

我有一个数组,其中包含来自数据库的大量数据。 在数组中,有两个字段,称为teameinsidteamzweiid 该查询是一个非常简单的查询:

$spiel = mysql_query("select teameinsid, teamzweiid from begegnung", $connection) or die("Keine passende Begegnung");

我需要在数据库中搜索这些ID的名称。 名称在另一个表中。

我现在所拥有的是:

while($tmp = mysql_fetch_array($spiel)){
 $teins = $tmp['teameinsid'];
 $tzwei = $tmp['teamzweiid'];
}

所以我知道两个ID,但是我不知道将名称保存在何处。 如果我尝试:

$name = mysql_query("select name from team where teameinsid = $teins", $con) 

每次都会被覆盖。 我该如何处理?

编辑:

数据库方案:

表团队:ID,名称表身份:ID,teameinsid,teamzweiid

我对此进行了测试,如果您在$ speil中的查询为:

SELECT B.*, T1.name AS teamnameeins, T2.name as teamnamezwei FROM begegnung AS B
JOIN team AS T1 ON T1.id = B.teameinsid
JOIN team AS T2 ON T2.id = B.teamzweiid

http://dev.mysql.com/doc/refman/5.0/en/join.html

您现在应该像这样使用代码:

while($tmp = mysql_fetch_array($spiel)){
 $teins = $tmp['teamnameeins'];
 $tzwei = $tmp['teamnamezwei'];
}

像这样改变你的代码

while($tmp = mysql_fetch_array($spiel)){
     $teins[] = $tmp['teameinsid'];
     $tzwei[] = $tmp['teamzweiid'];
    }

现在

$ids=implode ( ',' ,  $teins);

$name = mysql_query("select name from team where teameinsid in($ids)", $con) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM