Select query using referential tables...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quince
Forum Newbie
Posts: 15
Joined: Tue Jul 05, 2005 10:17 am

Select query using referential tables...

Post by quince »

i have a user table and then a user_groups table that references the user table I want to get the list of users that are part of a specific group. I can query the group and get the user_id that is reference, but then I want it to give me that user name that goes whit it. Does anyone know how to write this type of query or do I need to make 2 queries? where the 1st query gets the user_id's from the user_gruops table and the second takes the user_id's and get the user_names from the users table.

Thanks for the help!
Quince
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you just need to "join" the two tables. You could do it with an Inner join, or something like this:

Code: Select all

select u.username, g.groupname from users u, groups g where u.id = g.userid
quince
Forum Newbie
Posts: 15
Joined: Tue Jul 05, 2005 10:17 am

Post by quince »

thanks I'll try that. :P
quince
Forum Newbie
Posts: 15
Joined: Tue Jul 05, 2005 10:17 am

Post by quince »

I guess I'm still a little confused...

here is what I tried to do:

Code: Select all

$result = mysql_query(&quote;SELECT u.username, g.user_id FROM user u, user_group g WHERE user_group=&quote;group1&quote;)
but where do I relate the user_id?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$result = mysql_query(&quote;SELECT u.username, g.user_id FROM user u, user_group g WHERE g.user_group='group1' and u.id = g.user_id&quote;)
quince
Forum Newbie
Posts: 15
Joined: Tue Jul 05, 2005 10:17 am

Post by quince »

Thanks! U DA MAN! :lol:
Post Reply