【发布时间】:2011-12-28 16:46:44
【问题描述】:
我需要查询location = $location 所在的数据库。这将返回一些user_ids。
然后我需要查询另一个数据库,其中user_id = 那些以前的user_id 结果。这将找到game_ids。
然后我需要用game_ids 查询另一个表以获取每个游戏的名称(每个游戏名称都分配有一个 id。)
这是我所拥有的:
if (isset($_POST['location'])) {
$location = $_POST['location'];
$query = mysql_query("SELECT * FROM users WHERE location = '$location'") or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($query)) {
foreach (explode(", ", $row['user_id']) as $users) {
echo "<li>" . $row['user_name'] . "<ul><li>";
//this where I need I need to query another table where user_Id = $user (each user found in previous query)
//this will find multiple game id's (as $game) , I then need to query another table to find game_id = $game
// output name of game
echo "</li></ul></li>";
}
}
echo "</ul>";
}
听起来很复杂。有没有更简单的方法?
【问题讨论】:
-
学习在数据库查询中使用 JOIN,您会发现它的效率要高得多,但如果您规范化数据库而不是将多个值存储在 user_id 之类的列中也会有所帮助