[英]For all entries in one table, add to another table
Database looks like this:数据库看起来像这样:
table1表格1
id | name
1 | James
2 | Rick
3 | Carl
table2表2
id | age | gender
<this table is empty>
I want to make a query that passes the same data for all ID's into table2.我想进行一个查询,将所有 ID 的相同数据传递到表 2 中。 So after the query the database would look like the following:所以在查询之后数据库将如下所示:
table1表格1
id | name
1 | James
2 | Rick
3 | Carl
table2表2
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.我试图进行一些 INNER JOIN 查询,但我似乎无法使其工作。 Any suggestions?有什么建议么?
Do you want this?你想要这个吗?
insert into table2 (id, age, gender)
select id, 20, 'male'
from table1;
Normally, id
s are defined automatically, so you can probably leave that out:通常, id
是自动定义的,因此您可以忽略它:
insert into table2 (age, gender)
select 20, 'male'
from table1;
It looks like you want id
s from table1
, and then fixed values for other columns.看起来您想要table1
中的id
,然后是其他列的固定值。
If so, consider the insert... select
syntax:如果是这样,请考虑insert... select
语法:
insert into table2 (id, age, gender)
select id, 20, 'male' from table1
You can do it like described here:你可以像这里描述的那样做:
https://www.w3schools.com/sql/sql_insert_into_select.asp 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.