PDA

View Full Version : mysql query help



Todd W
12-17-2008, 11:28 AM
I have a table for user labels it contains the user_id, the group_id, and the status.

I am building a search page that will allow the admins to find users who belong to a label and also another label (or NOT) another label.

I understand obviously how to select * where group_id='#' but how do I make sure that the user_id for the returned row also belongs to the 2nd selected user group (or doesn't belong for that matter)?

Chris
12-21-2008, 07:27 AM
that is an interesting problem, I've never done a query like that.

I am not 100% sure it is possible with one query with your table setup as it is.

where group_id="1" and group_id="2" doesn't work does it?

The problem is, you have 1 field.

I think you either have to do this with a nested query. I'm just guessing here so not sure this'll work.

select users.user_id, users.group_id, 2users.group_id. FROM users, (select users.group_id from users where group_id = "2") as 2users WHERE users.group_id = "1"

the other option is to change the table and store the group values as a list in one field.

http://dev.mysql.com/doc/refman/5.1/en/unnamed-views.html

skysboy
12-23-2008, 05:56 AM
just pass