简体   繁体   English

对于一个表中的所有条目,添加到另一个表中

[英]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.

相关问题 使用硬编码值将所有条目从一个表复制到另一个表 - Copying all entries from one table to another with a hard coded value 从一个表中获取所有条目以及在另一个表中的这些条目的计数 - Get all entries from one table together with the count of these entries in another table 从一个表中获取所有不在另一个特定表中的mysql条目 - get all mysql entries out of of one table which are not in another specific table 根据mysql中另一个表中的新条目在一个表中添加新列 - Add new columns in one table based on new entries in another table in mysql SQL筛选器条目与另一个表中的所有条目匹配 - SQL filter entries which match all entries from another table 在cakephp中基于另一个表获取一个表的条目 - Get entries of one table based on another table in cakephp 来自一个表的MYSQL条目最常出现在另一表中 - MYSQL entries from one table that appear most often in another table 根据另一个表上的条件在一个MySQL表中查找条目 - Finding entries in one MySQL table based on conditions on another table 根据另一个表中的计数条目更新一个表中的列值 - Update Column Value in one table based on count entries in another table 将一个表中的动态条目数存储到另一个表中的单个行 - Store dynamic number of entries in one table to a single row in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM