How do I do this select?

This is a discussion on How do I do this select? within the PHP General forums, part of the PHP Programming Forums category; I sent this to the mysql list but it doesn't seem to be working so I was hoping someone ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-11-2004
Chris W
 
Posts: n/a
Default How do I do this select?

I sent this to the mysql list but it doesn't seem to be working so I was
hoping someone here could help.

I have the following 2 tables.

CREATE TABLE User (
UserKey INT NOT NULL AUTO_INCREMENT,
UserID CHAR(16) NOT NULL UNIQUE ,
Name VARCHAR(20),
PRIMARY KEY (UserKey)
);

CREATE TABLE FriendList(
UserKey INT NOT NULL,
FriendKey INT NOT NULL,
PRIMARY KEY (UserKey, FriendKey)
);

The second table is a many to many relationship table. I want to select
every row from FriendList and link it to userID so the out put looks
something like this.

UserID FriendID FriendName
a x x-name
a y y-name
a z z-name
b a a-name
b c c-name
b z z-name
c b b-name
c x x-name


This will give me sort of what I am looking for, but it shows the
UserKey in the first column and I want the UserID. I have tried a few
others but just got errors.

SELECT f.UserKey, UserID FriendID, Name FriendName
FROM User u, FriendList f
WHERE f.FriendKey = u.UserKey
ORDER BY f.UserKey, FriendID;
Reply With Quote
  #2 (permalink)  
Old 01-11-2004
Manuel VáZquez Acosta
 
Posts: n/a
Default Re: How do I do this select?

select distinct user.userId, fu.UserID as FriendID, fu.Name as FriendName
from user, friendlist as f, user as fu
where f.userkey = user.userkey and f.friendkey = fu.userkey
order by user.userId, friendId;


"Chris W" <1qazse4@cox.net> wrote in message
news:40015A03.2060505@cox.net...
> I sent this to the mysql list but it doesn't seem to be working so I was
> hoping someone here could help.
>
> I have the following 2 tables.
>
> CREATE TABLE User (
> UserKey INT NOT NULL AUTO_INCREMENT,
> UserID CHAR(16) NOT NULL UNIQUE ,
> Name VARCHAR(20),
> PRIMARY KEY (UserKey)
> );
>
> CREATE TABLE FriendList(
> UserKey INT NOT NULL,
> FriendKey INT NOT NULL,
> PRIMARY KEY (UserKey, FriendKey)
> );
>
> The second table is a many to many relationship table. I want to select
> every row from FriendList and link it to userID so the out put looks
> something like this.
>
> UserID FriendID FriendName
> a x x-name
> a y y-name
> a z z-name
> b a a-name
> b c c-name
> b z z-name
> c b b-name
> c x x-name
>
>
> This will give me sort of what I am looking for, but it shows the
> UserKey in the first column and I want the UserID. I have tried a few
> others but just got errors.
>
> SELECT f.UserKey, UserID FriendID, Name FriendName
> FROM User u, FriendList f
> WHERE f.FriendKey = u.UserKey
> ORDER BY f.UserKey, FriendID;

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:10 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0