How to join two tables

后端 未结 1 1647
醉话见心
醉话见心 2021-01-29 16:29

Table1

Date        v1  
05/01/2010  26          
05/02/2010  31   
05/03/2010  50  

Table2

Date        v2 v3  
05/01/2010  42         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 17:00

    You use the JOIN keyword:

    SELECT table2.Date, COALESCE(v1, 0) AS v1, v2, v3
    FROM Table2
    LEFT JOIN Table1
    ON table1.Date = table2.Date
    

    There are different types of join for example:

    • INNER JOIN
    • LEFT OUTER JOIN
    • RIGHT OUTER JOIN
    • FULL OUTER JOIN

    If you don't specify which type of join you want, by default you get an INNER join. It seems here that you want a LEFT JOIN or a FULL OUTER JOIN although it isn't clear which from your question.

    See this question for an explanation of the different types of joins:

    • What is the difference between Left, Right, Outer and Inner Joins?

    0 讨论(0)
提交回复
热议问题