如何在通过连接连接的表中使用访问数据?

时间:2013-06-05 00:21:47

标签: mysql sql join

我有一个网站的一部分,只有某些用户才能访问。我在想这个用于访问控制的表结构。有没有其他方式可以获得更好的表现?

**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)可以访问的所有部分?

谢谢

5 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分: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

答案 2 :(得分:0)

第一个问题; Stack Overflow不是正确的地方。它更适合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

答案 3 :(得分:0)

尝试:

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

答案 4 :(得分:0)

SELECT *
FROM access_control
JOIN sections ON sections.id = access_control.section_id
WHERE access_control.user_id =39