2013-06-05 48 views
-1

我有一個網站的部分應該只能被某些用戶訪問。我正在考慮訪問控制的這個表結構。有沒有其他更好的表現方式?如何使用通過連接連接的表中的訪問數據?

**Users** 
id | Name | age 
39 | Peter | 24 
40 | Alan | 15 


**Sections** 
id | Name | description 
1  | Games | flash games 
2  | Bank | bank access 


**AccessControl** 
id | user_id | section_id 
1 |  39  | 1 
2 |  39  | 2 
3 |  40  | 1 

SELECT將如何獲得Peter(id:39)的所有部分都可以訪問?

謝謝

回答

1

試試這個:

SELECT * FROM Sections where section_id in (SELECT section_id from AccessControl where user_id=39) 
0

關於嘗試如何:

select s.* from Users u, Sections s, AccessControl ac where 
u.id = 39 and 
u.id = ac.user_id and 
ac.section_id = s.id 
0

關於第一個問題;堆棧溢出不是正確的地方。它更適合於Database Administrators。關於第二個問題:

SELECT s.name, s.description, u.name 
FROM Sections s, Users u 
INNER JOIN AccessControl ac 
    ON (ac.user_id = u.id 
     AND ac.section_id = s.id) 
WHERE u.id = 39 
0

嘗試:

SELECT * 
FROM Sections 
JOIN AccessControl ON Sections.id = AccessControl.section_id 
JOIN Users ON Users.id = AccessControl.user_id 
WHERE Users.id = 39 
0
SELECT * 
FROM access_control 
JOIN sections ON sections.id = access_control.section_id 
WHERE access_control.user_id =39