通过SQL左连接在另一个左连接上排序

时间:2015-09-25 09:58:03

标签: php mysql sql join

我正在尝试使用join来获取3个不同表中的某些列。我做了一个直观的表示,显示了表在SQL语句中的连接方式。我希望在rl.level_priority表中按role_level订购结果。

FROM users u LEFT JOIN
                user_roles ur
                ON u.id = ur.user_id LEFT JOIN
                role_level rl
                ON ur.role_level_id = rl.id AND ur.role_id = ?
                WHERE (u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null)
                GROUP BY u.id, u.fullname, u.image_thumb, u.group_id
                ORDER BY rl.level_priority

我很难看到我是否正确地做到了这一点。我得到了结果,但他们没有按照rl.level_priority

排序

Left joining a left join

整个代码在这里:

while($stmt_two->fetch()){
                // HIGHEST USER ROLE DISPLAYED - ACTIVE (6), LESS ACTIVE(7), PASSIVE (9)
                $db_user_group_id = 7;
                // DISPLAY USERS WITH A SET JOB POSITION
                $sql_three = <<< EOF
                SELECT u.id, u.fullname, u.image_thumb, u.group_id, MIN(rl.level_priority)
                FROM users u LEFT JOIN
                user_roles ur
                ON u.id = ur.user_id LEFT JOIN
                role_level rl
                ON ur.role_level_id = rl.id AND ur.role_id = ?
                WHERE (u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null)
                GROUP BY u.id, u.fullname, u.image_thumb, u.group_id
                ORDER BY rl.level_priority;
EOF;
                $stmt_three = $mysqli->prepare($sql_three) or die ("Feil i database<br>" . $sql . "<br><b>Feilmelding:</b> " . $mysqli->error);
                $stmt_three->bind_param("iii",$db_user_role_id, $db_user_group_id, $db_role_id);
                $stmt_three->execute();
                $stmt_three->store_result();
                $stmt_three->bind_result($db_id_role, $db_fullname_role, $db_image_thumb_role, $db_group_id, $db_level_priority);
                $results_users_role = $stmt_three->num_rows;
                $results += $results_users_role;

                if (!$results_users_role == 0) {
                    while($stmt_three->fetch()) {
                        $body_html .= "<tr>";
                        if (!empty($db_image_thumb_role)) {
                            $body_html .= "<td><img src=\"data:image/jpeg;base64," . base64_encode($db_image_thumb_role) . "\"/></td>\n";
                        }
                        else {
                            $body_html .= "<td><img src=\"img/anonymous-user-thumb.png\"></td>";
                        }

                        $body_html .= "<td><a href=user.php?u=" . $db_id_role . ">" . $db_fullname_role . "</a></td></tr>";
                    }
                }
                $stmt_three->close();
            }

3 个答案:

答案 0 :(得分:1)

这是您的查询:

SELECT u.id, u.fullname, u.image_thumb, u.group_id, MIN(rl.level_priority)
FROM users u LEFT JOIN
     user_roles ur
     ON u.id = ur.user_id LEFT JOIN
     role_level rl
     ON ur.role_level_id = rl.id
WHERE (u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null) AND
      ur.role_id = ?
GROUP BY rl.level_priority;

您的WHERE子句正在撤消LEFT JOIN(这是将其变为INNER JOIN),因为对于不匹配,ur.role_id将为NULL。如果您确实需要LEFT JOIN,请将此条件移至ON子句。

其次,你有GROUP BY子句,但那里有错误的列。正确的列是聚合函数中的列。最后,您需要ORDER BY

所以:

SELECT u.id, u.fullname, u.image_thumb, u.group_id, MIN(rl.level_priority)
FROM users u LEFT JOIN
     user_roles ur
     ON u.id = ur.user_id LEFT JOIN
     role_level rl
     ON ur.role_level_id = rl.id AND ur.role_id = ?
WHERE (u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null) 
GROUP BY u.id, u.fullname, u.image_thumb, u.group_id
ORDER BY rl.level_priority;

答案 1 :(得分:0)

尝试这个

SELECT u.id, u.fullname, u.image_thumb, u.group_id, ur.role_level, MIN(rl.level_priority)
FROM users u
  LEFT JOIN user_roles ur ON u.id = ur.user_id
  LEFT JOIN role_level rl ON ur.role_level_id = rl.id
WHERE ((u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null)) AND
ur.role_id = ?
GROUP BY rl.level_priority
order by rl.level_priority

答案 2 :(得分:0)

试试这个:

SELECT u.id, u.fullname, u.image_thumb, u.group_id, ur.role_level, MIN(rl.level_priority)
FROM users u
  LEFT JOIN user_roles ur ON u.id = ur.user_id
  LEFT JOIN role_level rl ON ur.role_level_id = rl.id
WHERE ((u.id = ?) AND (u.group_id <= ?) AND (u.db_isdeleted is null)) AND ur.role_id = ?
GROUP BY rl.level_priority
order by MIN(rl.level_priority)