SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 06-07-2006, 04:24 PM
Scott
 
Posts: n/a
Default Oracle dialect question:

Do two joins in a single query?


Table structure as follows:

'people' table:
id | name
---------------
1 | scott
2 | jim
3 | bob

'projects' table:
project_name | developer | requestor
-------------------------------------
project1 | 1 | 2
project2 | 1 | 3


So basically I want to join more than one value from the 'people' table into a query on the 'projects' table. I'd like to get something like this:

result:
project_name | developer | requestor
-------------------------------------
project1 | scott | jim
project2 | scott | bob

Is this possible in one SQL statement?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 06-08-2006, 02:41 AM
Dimitar
 
Posts: n/a
Default Oracle answer. Re: Do two joins in a single query?

In Oracle you could do this:
SELECT
 projects.name,
 (SELECT name FROM people WHERE id=projects.developer) AS "developer",
 (SELECT name FROM people WHERE id=projects.requestor) AS "requestor"
FROM
 projects
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 06-08-2006, 03:16 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: Do two joins in a single query?

For some reason you might prefer this:
SELECT
 projects.name,
 developer.name AS "developer",
 requestor.name AS "requestor"
FROM
 projects
JOIN people developer ON (developer.id = projects.developer)
JOIN people requestor ON (requestor.id = projects.requestor)

Consider however the type of join you need (perhaps "left" one?).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Moderation Tools:


Similar Threads
Thread Thread Starter Forum Replies Last Post
can i wrire a query which sum up more than 2 columns in a single table? kajal SQL - Questions and Answers 2 03-06-2007 12:41 AM
How to use two joins in a single query? Vaibhav Pingl SQL - Questions and Answers 1 11-29-2006 05:59 PM


All times are GMT. The time now is 04:54 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7