SQL INSERT INTO from multiple tables

前端 未结 7 670
清歌不尽
清歌不尽 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 13:05

    Here is an example if multiple tables don't have common Id, you can create yourself, I use 1 as commonId to create common id so that I can inner join them:

    Insert Into #TempResult
    select CountA, CountB, CountC  from
    
    (
    select Count(A_Id) as CountA, 1 as commonId from tableA
      where ....
      and  ...
      and   ...
    ) as tempA
    
    inner join
    (
    select Count(B_Id) as CountB, 1 as commonId from tableB
      where ...
      and ...
      and  ...
     ) as tempB
    
    on tempA.commonId = tempB.commonId
    
    inner join
    (
    select Count(C_ID) as CountC, 1 as commonId from tableC
    where ...
    and   ...
    ) as tempC
    
    on tmepB.commonId = tempC.commonId
    
    --view insert result
    select * from #TempResult
    

提交回复
热议问题