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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog