Answered Sql server join

  • Sunday, March 17, 2013 3:41 AM
     
     

    hi,
    i had three tables, they are NonReg,Reg,Bookings
    in Booking table like this

    BookingID Bookingdate  UID  isReg
    1                  12/03/2012    10   0
    2                   11/03/2012   10   1

    for NonReg Table like this

    UID   UNAME
    10    bavaji

    for   Reg Table like this

    UID   UNAME

    10    basha

    how to perfom the join for these three tables
    i.e. when isReg is 0 then perform the join to Booking table and NonReg Tables
    and  when isReg is 1 then perform the join to Booking table and Reg Tables

All Replies

  • Sunday, March 17, 2013 3:47 AM
    Moderator
     
     Answered Has Code

    The simplest way

    select B.*, NR.UName
    
    from Booking B INNER JOIN NonReq NR ON B.UID = NR.UID
    
    WHERE B.isReq = 0
    
    UNION ALL
    
    select B.*, R.UName
    
    from Booking B INNER JOIN Req R ON B.UID = R.UID
    
    WHERE B.isReq = 1
    



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

  • Sunday, March 17, 2013 4:55 AM
     
     

    Thank you, :) :) :)