MySQL: Combining multiple where conditions

Posted by Karl on Stack Overflow See other posts from Stack Overflow or by Karl
Published on 2010-06-06T20:13:17Z Indexed on 2010/06/06 20:22 UTC
Read the original article Hit count: 201

Filed under:

I'm working on a menu system that takes a url and then queries the db to build the menu.
My menu table is:

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment | 
| node_id | int(11)      | YES  |     | NULL    |                | 
| parent  | int(11)      | YES  |     | NULL    |                | 
| weight  | int(11)      | YES  |     | NULL    |                | 
| title   | varchar(250) | YES  |     | NULL    |                | 
| alias   | varchar(250) | YES  |     | NULL    |                | 
| exclude | int(11)      | YES  |     | NULL    |                | 
+---------+--------------+------+-----+---------+----------------+

The relevant columns for my question are alias, parent and node_id.
So for a url like: http://example.com/folder1/folder2/filename
Alias would potentially = "filename", "folder1", "folder2"
Parent = the node_id of the parent folder.

What I know is how to split the url up into an array and check the alias for a match to each part. What I don't know is how to have it then filter by parent whose alias matches "folder2" and whose parent alias matches "folder1". I'm imagining a query like so:

select * from menu 
where alias='filename' and 
where parent = node_id 
where alias='folder2' and parent = node_id 
where alias='folder1'

Except I know that the above is wrong. I'm hoping this can be done in a single query.

Thanks for any help in advance!

© Stack Overflow or respective owner

Related posts about mysql