Reputation: 753
I want to copy two columns from table 1 into table 2, filling another two columns in table 2 with fixed strings. Here's what I've attempted:
table1 has columns: name, age, child, adult
table2 has columns: name, param, xvalue, yvalue
INSERT INTO table2 (name, param, xvalue, yvalue)
VALUES ('John', 'age',
SELECT child FROM table1 WHERE name='John',
SELECT adult FROM table1 WHERE name='John');
But it gives a missing expression
error.
The result should have table2 name
column be John
for all rows, and likewise the param
column should be age
for all rows. Table 2 xvalue
and yvalue
columns should contain the selected data from table 1.
Any idea how to do this?
Upvotes: 0
Views: 634
Reputation: 1269443
Use insert . . . select
:
INSERT INTO table2 (name, param, xvalue, yvalue)
SELECT 'John', 'age', child, adult
FROM table1
WHERE name = 'John';
Upvotes: 2