user46688
user46688

Reputation: 753

SQL: copy data from one table to another while adding additional columns of fixed values

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

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

Use insert . . . select:

INSERT INTO table2 (name, param, xvalue, yvalue) 
     SELECT 'John', 'age', child, adult
     FROM table1 
     WHERE name = 'John';

Upvotes: 2

Related Questions