1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

Do two joins in a single query?

Discussion in 'SQL - Questions and Answers' started by Scott, Jun 7, 2006.

  1. Scott Guest

    Dialect: Oracle
    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?
  2. Dimitar Guest

    Dialect: Oracle
    In Oracle you could do this:
    SQL:

    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
  3. Dimitar Guest

    Dialect: SQL 92
    For some reason you might prefer this:
    SQL:

    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?).

Share This Page