View Single Post

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

On 15 mar, 10:44, "Captain Paralytic" <paul_laut...@yahoo.com> wrote:
> 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`



This returned only 3 records, that is the number of users in the table
users. But in tabel users_groups I have 5 records... =((( This was
basicaly the same statement I wrote earlier... =(((

Reply With Quote