MySQL: Joining menu table, user table, access table
Well guys, I have three tables here, menu, user and access. For simplifying let's consider:
- User table has one column: id
- Menu table has 5 columns: id, title, url, lft, rgt
- Access table has 4 colums: id, user_id, menu_id, access
I'm using SitePoint's Modified Preorder Tree Traversal to display the menu:
SELECT id, t开发者_高级运维itle, lft, rgt, url FROM menu WHERE lft BETWEEN 3 AND 49 ORDER BY lft ASC;
In the access table, the access column is having values of 0 or 1. At the same time, on a given page, I have the id value of User table, say $id.
The idea is modify the above select to show only those menu items, where in the corresponding Access table, the access column is set to 1. Where access = 0, the menu doesn't turn up for the given user.
Have I been clear? Thanks in advance for any help. Cheers.
Select ...
From menu
Where lft Between 3 And 49
And Exists (
Select 1
From AccessTable As T1
Where T1.menu_id = menu.Id
And T1.access = 1
And T1.user_id = $id
)
select id, title, lft, rgt, url from menu join access on (menu.id = access.menu_id) where (lft between 3 and 49) and access.access=1 order by lft asc;
精彩评论