2012-09-12 164 views
0

我在加入sql時遇到了問題。 我有3張桌子。在Sql服務器中加入查詢

1: Lists the user details 
2: Lists the permissions the user group has 
3: Lists the page that that group can access 

 

Table1 users : 
    **************************************** 
    username | group 
    **************************************** 
    admin | administrator 


    Table2 groups : 
    ********************************************* 
    user_group | create | view | system_admin 
    ********************************************* 
    administrator | 1 | 0 |  1 


    Table3 urls: 
    ********************************************* 
    create  | view  | system_admin 
    ********************************************* 
    create.php | view.php | system.php 

(我的表格繪製道歉)

我通過PHP做什麼,是抓住他們屬於USER_GROUP。 然後,我需要檢查他們是否有權訪問他們剛剛點擊的頁面或將它們重定向回來。

我可以通過當前表格佈局來實現它們通過連接的方式嗎?或者我應該重新設計這些表格,因爲它們對於這類事物並不直觀。

+0

怎麼辦你把'urls'表與其他人聯繫起來? – Taryn

回答

2

實際上我重新設計表格,以使它們更容易查詢:

create table users 
(
    id int, 
    username varchar(10), 
    groupid int 
); 
insert into users values (1, 'admin', 1); 

create table groups 
(
    groupid int, 
    groupname varchar(20) 
); 
insert into groups values (1, 'administrator'); 

create table permissions 
(
    permissionid int, 
    permissionname varchar(20) 
); 
insert into permissions values (1, 'create'); 
insert into permissions values (2, 'view'); 
insert into permissions values (3, 'system_admin'); 

create table urls 
(
    urlid int, 
    name varchar(10) 
); 
insert into urls values(1, 'create.php'); 
insert into urls values(2, 'view.php'); 
insert into urls values(3, 'system.php'); 

create table group_permission_urls 
(
    groupid int, 
    permissionid int, 
    urlid int 
); 
insert into group_permission_urls values(1, 1, 1); 
insert into group_permission_urls values(1, 0, 2); 
insert into group_permission_urls values(1, 3, 3); 

然後將查詢將類似於此:

select * 
from users us 
left join groups g 
    on us.groupid = g.groupid 
left join group_permission_urls gpu 
    on us.groupid = gpu.groupid 
left join permissions p 
    on gpu.permissionid = p.permissionid 
left join urls u 
    on gpu.urlid = u.urlid 

看到SQL Fiddle with Demo

0

通過比較$current_pageIN()子查詢的結果,您可以在一個查詢中執行此操作。如果頁面與用戶有權限列中列出的任何頁面相匹配,則會返回一行。如果允許列中沒有匹配,它不應該返回任何行。

SELECT 
    groups.create, 
    groups.view, 
    groups.system_admin, 
    1 AS can_access 
FROM 
    users 
    JOIN groups ON users.group = groups.user_group 
WHERE 
    users.username = '$some_username' 
    AND (
    /* Substitute the current page. Better, use a prepared statement placeholder if your API supports it */ 
    (groups.create = 1 AND '$current_page' IN (SELECT DISTINCT create FROM urls)) 
    OR 
    (groups.view = 1 AND '$current_page' IN (SELECT DISTINCT view FROM urls)) 
    OR 
    (groups.system_admin = 1 AND '$current_page' IN (SELECT DISTINCT system_admin FROM urls)) 
) 

此作品由$current_page比較從每個3列的不同可能值的集合。如果它與列匹配,並且用戶的組具有該類型的權限,則返回一行。

0
select case when count(1) > 0 then 'come in' else 'go away' end 
from users, groups, urls 
where 
users.username = '$username' and 
users.user_group = groups.user_group and 
    ((urls.create = '$url' and groups.create = 1) or 
    (urls.view = '$url' and groups.view = 1) or 
    (urls.system_admin = '$url' and groups.system_admin = 1))