我有以下代码可以使用,但我知道它不是很有效,我可以将它合并到一个SQL查询中。我应该打扰吗?如果是这样,怎么样?
function get_rnds_by_judge($judge_id) {
global $connection;
$sql = "SELECT *
FROM Round_User
LEFT JOIN Round
ON Round_User.RndID = Round.RndID
WHERE Round_User.UserID = '$judge_id'
";
$rnd_set = mysql_query($sql, $connection);
confirm_query($rnd_set);
return $rnd_set;
}
function get_users_by_round_and_role($round_id, $role_id) {
global $connection;
$sql = "SELECT *
FROM User
LEFT JOIN Round_User
ON User.UserID = Round_User.UserID
WHERE Round_User.RndID = '$round_id' and User.UserRoleID = '$role_id'
";
$rnd_set = mysql_query($sql, $connection);
confirm_query($rnd_set);
return $rnd_set;
}
$rnd_set = get_rnds_by_judge($_SESSION[UserID]);
while($rnd_row = mysql_fetch_array($rnd_set)){
echo "<li data-role=\"list-divider\">";
echo $rnd_row[RndTitle];
echo "</li>";
$rs_set = get_users_by_round_and_role($rnd_row[RndID], 7);
while($rs_row = mysql_fetch_array($rs_set)){
echo "<li>";
echo "<a href=\"#\">{$rs_row[UserFirstName]}</a>";
echo "</li>";
}
}
我绊倒的部分是如何从我的加入RndID
表中获取UserID
和Round_User
并从两个不同的表中获取数据然后将它们全部重新组合在一起。
答案 0 :(得分:0)
如果没有看到表格结构和示例数据,很难说,但你可能正在寻找类似这样的东西
SELECT u.*
FROM User u LEFT JOIN
Round_User ru ON u.UserID = ru.UserID
WHERE ru.RndID IN (
SELECT ru2.RndId
FROM Round_User ru2 LEFT JOIN
Round r ON ru2.RndID = r.RndID
WHERE ru2.UserID = '$judge_id')
AND u.UserRoleID = 7
查询显然尚未经过测试。