Jake
Jake

Reputation: 403

For all entries in one table, add to another table

Database looks like this:

table1

id   | name
1    | James
2    | Rick
3    | Carl

table2

id   | age | gender
<this table is empty>

I want to make a query that passes the same data for all ID's into table2. So after the query the database would look like the following:

table1

id   | name
1    | James
2    | Rick
3    | Carl

table2

id   | age | gender
1    | 20  | male
2    | 20  | male
3    | 20  | male

I've tried to make some INNER JOIN queries, but I can't seem to make it work. Any suggestions?

Upvotes: 1

Views: 482

Answers (3)

Patrick
Patrick

Reputation: 948

You can do it like described here:

https://www.w3schools.com/sql/sql_insert_into_select.asp

could be something like

INSERT INTO table2 (id, age, gender)
SELECT id,
       20,
       'male'
FROM table1
WHERE 1 = 1;

Upvotes: 0

GMB
GMB

Reputation: 222502

It looks like you want ids from table1, and then fixed values for other columns.

If so, consider the insert ... select syntax:

insert into table2 (id, age, gender)
select id, 20, 'male' from table1

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269993

Do you want this?

insert into table2 (id, age, gender)
    select id, 20, 'male'
    from table1;

Normally, ids are defined automatically, so you can probably leave that out:

insert into table2 (age, gender)
    select 20, 'male'
    from table1;

Upvotes: 3

Related Questions