SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    If foreign key is existed,

    Code:
    myTable1
    
    (myTable1_ID) (myTable1_message)
    
    (1)             I am a boy
    (2)             you are a girl
    (3)             They are students 
    
    
    
    myTable2
    
    (myTable2_ID) (myTable2_message)
    
    (1)            I am Tom
    (3)            They are Tom and Judy
    mytable2_ID in myTable2 is connected myTable1_ID in myTable1.

    I like to produce like the following.

    If myTable2_ID responding to myTable1_ID is existed,
    produce myTable2_message,

    If myTable2_ID responding to myTable1_ID isn't existed,
    produce myTable1_message.


    Target result would be like the following.

    Code:
    target result
    
    (1) I am Tom
    (2) you are a girl
    (3) They are Tom and Judy
    Thanks in Advance

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select myTable1_ID
         , coalesce(myTable2_message,myTable1_message) as message
      from myTable1
    left outer
      join myTable2
        on myTable1_ID = myTable2_ID
    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
  •