locked
How to write a Sql querry for two tables RRS feed

  • Question

  • User1756125984 posted

    Hi,

    I have two tables, table1, table2,

    In table1 has id, idname and

    id, idname
    1,name1
    2,name2
    3,name3
    4,name4
    5,name5
    6,name6

    table 2 has id, year, count, month.

    id,year,count,month
    1,2012,10,2
    3,2012,5,2
    4,2012,15,4

    I need to write a sql querry to get the all id, idname from table1 and year count and month values from table2 using common id.

    Here in table2 all id are not exist like above example, i need to display  all ids from table1 and existing data from table2 like month wise.

    finally i  need result like

    id, idname, year,count, month
    1,name1,2012,10,2
    2,name2,null,null,null
    3,name3,2012,5,2
    4,name4,2012,15,4
    5,name5,null,null,null
    6,name6,null,null,null

     

    Thursday, September 13, 2012 7:03 AM

Answers

  • User-1356203182 posted

    select t1.*, t2.* from table1 t1 INNER JOIN table2 t2 on t1.id = t2.id

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2012 7:08 AM
  • User1429614832 posted

    In order to get all ids from table 1 and only existing data from table2 you can go for LEFT OUTER JOIN.

    SELECT t1.ID,t1.Name,t2.Country
    FROM table1 AS t1
    LEFT OUTER JOIN table2 AS t2
    ON t1.ID = t2.ID


    Thanks

    Tanuja

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2012 8:14 AM

All replies

  • User-1356203182 posted

    select t1.*, t2.* from table1 t1 INNER JOIN table2 t2 on t1.id = t2.id

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2012 7:08 AM
  • User1429614832 posted

    In order to get all ids from table 1 and only existing data from table2 you can go for LEFT OUTER JOIN.

    SELECT t1.ID,t1.Name,t2.Country
    FROM table1 AS t1
    LEFT OUTER JOIN table2 AS t2
    ON t1.ID = t2.ID


    Thanks

    Tanuja

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 13, 2012 8:14 AM