SQL INSERT INTO from multiple tables

前端 未结 7 651
清歌不尽
清歌不尽 2020-12-02 12:17

this is my table 1:

NAME       AGE        SEX        CITY             ID

Clara      22         f          New York         1
Bob        33         m                 


        
7条回答
  •  攒了一身酷
    2020-12-02 12:58

    Try doing:

    INSERT INTO table3(NAME,AGE,SEX,CITY,ID,NUMBER)
    SELECT t1.name,t1.age, t1.sex,t1.city,t1.id,t2.number
    FROM table1 t1
    LEFT JOIN table2 t2 ON t1.id = t2.id
    

    By using LEFT JOIN, this will insert every record from table 1 in table3, and for the ones that match the join condition in table2, it will also insert their number.

提交回复
热议问题