具有多个连接的分页查询

时间:2017-03-16 09:10:42

标签: php mysql join pagination

我有几个LEFT OUTER JOINS的查询,你可以在代码中看到我生成的对象包含2个填充了数组的对象,因为你知道我不能使用"简单"这里的分页因为对于一个用户我得到的列很少(对于每个角色和每个组)所以例如对于用户1我可以得到4-5个结果,因为用户可以有很多组和角色,我明白这是我无法进入的主要原因此查询使用LIMIT和offset,因为我会丢失一些信息。我的问题是,如何以每页20个不同用户及其所有角色和组的方式修改此查询。

$stmt = $mysqli->prepare("SELECT u.id, u.firstName, u.lastName, u.email, 
    u.phoneNumber, u.address, u.birthDate, ur.roleName, cg.id, cg.name FROM users as u 
    LEFT OUTER JOIN user_role as ur ON u.id = ur.userId 
    LEFT OUTER JOIN user_group as ug on ug.userId = u.id 
    LEFT OUTER JOIN control_group as cg on cg.id = ug.groupId WHERE u.id != ?");
$stmt->bind_param("i", $_SESSION["id"]);
$stmt->execute();
$stmt->bind_result($id, $firstName, $lastName, $email, $phoneNumber, 
    $address, $birthDate, $roleName, $groupId, $groupName);

$users = array();

while ($stmt->fetch()) {
    if (empty($users[$id])) {
        $users[$id] = array(
            'id' => $id,
            'firstName' => $firstName,
            'lastName' => $lastName,
            'email' => $email,
            'phoneNumber' => $phoneNumber,
            'address' => $address,
            'birthDate' => $birthDate,
            'roles' => array(),
            'groups' => array()
        );
    }
    if ($roleName) {
        $found = false;
        foreach ($users[$id]['roles'] as $role) {
            if($role['roleName'] == $roleName){
                $found = true;
                break;
            }
        }
        if($found == false)
            $users[$id]['roles'][] = array(
                'roleName' => $roleName
            );
     }

    if ($groupId) {
        $found = false;
        foreach ($users[$id]['groups'] as $group) {
            if($group['groupName'] == $groupName){
                $found = true;
                break;
            }
        }
        if($found == false)
            $users[$id]['groups'][] = array(
                'groupName' => $groupName
            );
     }
}

$res = []; 

foreach($users as $user) { 
    $res[] = $user; 
} 

$stmt->close();
$mysqli->close();
echo json_encode($res);

1 个答案:

答案 0 :(得分:1)

好吧,我认为你可以采用两种查询方法。 第一个你做分页

SELECT u.id FROM users LIMIT offset, count

第二个是你拥有的 u.id =(我认为它应该是=,而不是!=因为你有)改为u.id IN

像那样的Smth

$stmt = $mysqli->prepare("SELECT u.id from users u limit ?, ?");
$stmt->bind_param("1", $_SESSION["offset"]);
$stmt->bind_param("2", $_SESSION["limit"]);
$stmt->execute();
$stmt->bind_result($id);

$user_ids = array();

while ($stmt->fetch()) {
    $user_ids[] = $id;
}

$stmt2 = $mysqli->prepare("SELECT u.id, u.firstName, u.lastName, u.email, 
    u.phoneNumber, u.address, u.birthDate, ur.roleName, cg.id, cg.name FROM users as u 
    LEFT OUTER JOIN user_role as ur ON u.id = ur.userId 
    LEFT OUTER JOIN user_group as ug on ug.userId = u.id 
    LEFT OUTER JOIN control_group as cg on cg.id = ug.groupId WHERE u.id in (?)");
$stmt2->bind_param("1", implode(",", user_ids);
$stmt2->execute();
$stmt2->bind_result($id, $firstName, $lastName, $email, $phoneNumber, 
    $address, $birthDate, $roleName, $groupId, $groupName);

$users = array();

while ($stmt->fetch()) {
    ...
}