locked
show corresponding value from two tables.. RRS feed

  • Question

  • User944339287 posted

    Hi guys, not sure is it correct if asking this question here..

    I got two tables, how can i display all data from TABLE 1 and show TRUE if corresponding CID is found from TABLE 2.

    returned result....
    101 Apple      TRUE
    102 Mango    FALSE
    103 Grape      TRUE

    TABLE 1
    CID    Name
    101  Apple
    102  Mango
    103  Grape

    TABLE 2
    TID   CID
    1      101
    2      103

    Wednesday, April 2, 2014 6:34 AM

Answers

  • User1208776063 posted

    kengkit

    how can i display all data from TABLE 1 and show TRUE if corresponding CID is found from TABLE 2.
    SELECT A.CID, A.Name, 
           CASE WHEN B.TID IS NULL THEN 'false' ELSE 'true' END [Status] 
        FROM TABLE1 A LEFT OUTER JOIN TABLE2 B ON A.CID = B.CID
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 2, 2014 6:51 AM

All replies

  • User1208776063 posted

    kengkit

    how can i display all data from TABLE 1 and show TRUE if corresponding CID is found from TABLE 2.
    SELECT A.CID, A.Name, 
           CASE WHEN B.TID IS NULL THEN 'false' ELSE 'true' END [Status] 
        FROM TABLE1 A LEFT OUTER JOIN TABLE2 B ON A.CID = B.CID
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 2, 2014 6:51 AM
  • User944339287 posted

    Hi, what if the situation abit changed?

    Expected result... (when i want to display type = Fruit in TABLE2)

    101 Apple      TRUE
    102 Mango    FALSE
    103 Grape     TRUE

    TABLE1
    CID    Name
    101    Apple
    102    Mango
    103    Grape
    104    Chicken Chop
    105    Fish & Chip


    TABLE2
    TID   CID   type
    1      101   Fruit
    2      103   Fruit
    3      104   Meal

    * i have tried the following sql but not work as expected..

    SELECT A.CID, A.Name, 
    CASE WHEN B.TID IS NULL THEN 'false' ELSE 'true' END [Status] 
    FROM TABLE1 A LEFT OUTER JOIN TABLE2 B ON A.CID = B.CID WHERE B.type = 'Fruit'


    Wednesday, April 2, 2014 9:25 AM