If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to make this join select statement works

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-06-03, 19:10
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
How to make this join select statement works

Here is a select statement of table A:

select user01_id, user02_id from A where ...

I also need to have the names of users which is in the table user. How to join the table user to have the name field and yeild two fields with the same name?

select a.user01_id,u.name, a.user02_id, u.name from a join user u on (???)


table A

user01_id int,
user02_id int,
...
primary (user01_id, user02_id)


table user

userid int primary key,
name varchar(80),
...


Thans for your advise.
Reply With Quote
  #2 (permalink)  
Old 09-07-03, 07:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select user01_id, u01.name as name01
     , user02_id, u02.name as name02
  from A
left outer
  join user u01
    on user01_id = u01.userid
left outer
  join user u02
    on user02_id = u02.userid
rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 09-08-03, 17:58
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
Quote:
Originally posted by r937
Code:
select user01_id, u01.name as name01
     , user02_id, u02.name as name02
  from A
left outer
  join user u01
    on user01_id = u01.userid
left outer
  join user u02
    on user02_id = u02.userid
rudy
http://r937.com/
Hi, Rudy,

The query works well. I didn't know the alias also can be appled on a field in addition of a table. I guess I can use inner join instead when the both user01_id and user02_id of table A refer to the userid in the table user.

Thanks very much for your help.

Vernon
Reply With Quote
  #4 (permalink)  
Old 09-08-03, 19:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, you could use inner joins, if you are guaranteed that the userids will exist

i guess i use left outer simply because it is "defensive sql" -- if either of the userids doesn't match, the A row disappears....

rudy
Reply With Quote
  #5 (permalink)  
Old 09-09-03, 00:36
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
Quote:
Originally posted by r937
yes, you could use inner joins, if you are guaranteed that the userids will exist

i guess i use left outer simply because it is "defensive sql" -- if either of the userids doesn't match, the A row disappears....

rudy
In fact, the two IDs of table A are foreign keys of the tabe user primary key, userid. So existence of the userid is guaranteed.

A good usage of "defensive". That is the difference between a master and a regular craftsman.
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On