SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    589
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    assign allias on 2 left joins

    Hello Forums,

    I have this table

    user
    id first type
    1 jane A
    2 john B

    order_table
    id a_id b_id
    1 1 2

    I left joined order_table.a_id to user.id and order_table.b_id to user.id

    SELECT first FROM user
    LEFT JOIN user ON order_table.a_id = user.id AND order_table.b_id = user.id
    WHERE order_table.id = 1

    I need to create an alias for "first" to differentiate between types.
    Any ideas?

    Thanks
    Don't Panic

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    698
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select A.first, 
           B.first
      from user A
      join order_table
        on A.id = order_table.a_id
      join user B
        on order_table.b_id = B.id
     where order_table.id = 1

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    select A.first a_first, 
           B.first b_first
      from user A
      join order_table
        on A.id = order_table.a_id
      join user B
        on order_table.b_id = B.id
     where order_table.id = 1

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    sequence of tables in the FROM clause needs to be discombobulated
    Code:
    SELECT a.first AS a_first 
         , b.first AS b_first
      FROM order_table
    INNER  
      JOIN user AS a
        ON a.id = order_table.a_id
    INNER
      JOIN user AS b
        ON b.id = order_table.b_id
     WHERE order_table.id = 1
    FTFY
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •