I have two tables with the following schema,
- table1 (a int, b int, c int, d int default 123)
- table2 (a int, b int, c int, e int)
I would like to update the (a,b,c)
columns of table2
into table1
, but keep the default value of d
.
For example, with the following record
------------------------
table1 | a | b | c | d |
------------------------
| 1 | 2 | 3 | 4 |
------------------------
and
------------------------
table2 | a | b | c | e |
------------------------
| 5 | 6 | 7 | 8 |
------------------------
I would like an output of:
--------------------------
table1 | a | b | c | d |
--------------------------
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 123 |
--------------------------
Here is what I have tried:
INSERT INTO table1(a, b, c, d)
VALUES ((SELECT a, b, c FROM table2), DEFAULT)
This query has an invalid syntax, because it returns the table in the sub-query and uses it as a value.
I have also tried to avoid this solution below, because it hides the fact, that the default value of d
is used. I really want to explicitly make it default to make it clear.
INSERT INTO table1(a, b, c)
SELECT a, b, c
FROM table2
Is there a way to combine
INSERT .. VALUES(..)
with
INSERT.. SELECT.. FROM table
or just another way to achieve the same goal?