Home > Back-end >  Add entries of a table into rows of another table
Add entries of a table into rows of another table

Time:01-25

I have two tables

table a:

ID   VALUE_z
1    41
2    32
3    51

table b:

ID   TYPE z
1    a    10
1    b    15
1    c    20
2    a    12
2    b    8
2    c    5
3    a    21
3    b    4
3    c    2

I want to add the rows from table a to the column VALUE in table b based on the ID. The result should look like this

table result:

ID   TYPE VALUE
1    a    10
1    b    15
1    c    20
1    z    41
2    a    12
2    b    8
2    c    5
2    z    32
3    a    21
3    b    4
3    c    2
3    z    51

CodePudding user response:

Try the following using INSERT INTO SELECT Statement:

insert into tableB
select ID, 'z', VALUE_z
from tableA

See demo

  •  Tags:  
  • sql
  • Related