View Single Post

  #2 (permalink)  
Old 03-15-2007
Captain Paralytic
 
Posts: n/a
Default Re: Question about select statement

On 15 Mar, 13:32, "rics" <ricardo.ce...@gmail.com> wrote:
> Hello,
>
> I have 3 tables.
>
> * users
> -- userid
> -- username
>
> * groups
> -- groupid
> -- groupname
>
> * users_groups
> -- usergroupid
> -- userid
> -- groupid
>
> I wish to do a select that returns all the content of the
> *users_groups* but with usernames and groupnames in place of userid
> and groupid.
>
> My actual select only return 1 record per user. This is the code:
>
> select u.userid, u.username, g.groupname
> from users u, groups g, users_groups ug
> where u.userid = ug.userid and g.groupid = ug.groupid
>
> How can I get all the records in *users_groups* ?
>
> Any thougths?


SELECT
`ug`.`usergroupid`,
`g`.`groupname`,
`u`.`username`
FROM `users_groups` `ug`
JOIN `users` `u` ON `ug`.`userid` = `u`.`userid`
JOIN `groups` `g` ON `ug`.`groupid` = `g`.`groupid`

Reply With Quote