SQL表:
用户= ID,名称,电子邮件。
英雄= ID,名称,等级
所有者=用户ID,类Heroid

我有以下两个查询:

$result = mysql_query("SELECT heroid FROM owner WHERE userid='$id'");
$heroid=mysql_fetch_row($result);
$result2 = mysql_query("SELECT name FROM hero WHERE ID = 'heroid[0]'");

$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
..


可以通过单个查询完成吗?

最佳答案

是。你可以做:

SELECT h.name
FROM hero h
INNER JOIN owner o ON o.heroid = h.id
WHERE o.userid = $id

08-18 13:51