dtlh12053 2018-08-17 16:49
浏览 53
已采纳

从表中获取用户,其中group =另一个表行的组,但我有其他表行的id

MY USERS TABLE:

enter image description here

GROUP_PERMISSIONS TABLE

enter image description here

problem:

How can i get all the users(from users table) with the group i.e equal to the group of groups_permissions table but i have the gid of the group from groups_permissions table?

CODE:(that i have tried(it has some other functions also))

$stmt = $db_con->query("SELECT gp.*, s.*, uu.*, ug.* FROM (SELECT * FROM groups_permissions WHERE gid={$_POST['gp_row_id']}) gp, (SELECT * FROM settings WHERE id='1') s, (SELECT username FROM users ORDER BY id) uu, (SELECT username FROM users WHERE `group`=gp.group_name WHERE gp.gid={$_POST['gp_row_id']}) ug");            
$stmt->execute();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    /* FROM GROUP_PERMISSIONS TABLE */
        $response['id'] = $row['gid'];
        $response['group'] = $row['group_name'];
        $response['restricted_pages'] = $row['restricted_pages'];
        $response['restricted_permissions'] = $row['restricted_permissions'];
    /* FROM SETTINGS TABLE */
        $response['site_pages'] = $row['site_pages'];
        $response['site_permissions'] = $row['site_permissions'];
    /* FROM USERS TABLE */
        $active_ids[]= $row['username'];
        $userlist = join(",", $active_ids);
        $string = trim($userlist,'"');
        $response['users'] = $string;
    }
    echo json_encode($response);

FOR EXAMPLE if on submitting the form i get the row id 5 (see image above) of a group dsf from groups_permissions ,then i want to get all users with group dsf from users table but as i have the id for group from groups_permissions table then ,i dont know what to use but like Select username from users WHERE group='dsf' from permissions_table but rather select the group by the help of gid

  • 写回答

1条回答 默认 最新

  • douyan2821 2018-08-17 18:43
    关注

    If I got it right, select should be

    "SELECT *
    FROM users
    INNER JOIN permissions_table ON permissions_table.group_name = users.group
    WHERE permissions_table.gid = {$_POST['gp_row_id']}"
    

    And I would advice to keep group IDs in users table, instead of group names, as it is proper practice for relational databases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)