为来自另一个 table 的每个 ID 在 table 中插入行
Insert row in table for each id from another table
基本上我有两个这样的table:
Table 1: 用户
id_user, name, ...
Table 2: 叶子
id_2, employee (the column employee is the id of the user from the first table), ...
现在 table 两个是空的(没有行),对于第一个 table 的每个用户,我想在第二个用户上创建一行,插入来自首先 table 作为员工列中的值,例如:
Table 2: 叶子
id employee column1 column2 column3 column4 column5
id1 1 date1 date2 integer1 integer2 string
id2 2 date1 date2 integer1 integer2 string
...
我试过的插入:
这个很好用:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
VALUES (1, '2015-01-01', '2015-12-31', 3, 5, 'test');
这里我试了上面解释的方法,但是不行:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
VALUES ((SELECT id from users), '2015-01-01', '2015-12-31', 3, 5, 'test');
我收到以下错误:
#1242 - Subquery returns more than 1 row
我确实理解错误:子查询找到了多个结果,因此它无法将值插入员工,我的问题是我不知道作为菜鸟应该使用什么语法。我只想在第二个 table 中为第一个 table 中的每一行创建一行(使用第一个 table 中的 id)。
只需使用insert . . . select
:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
SELECT id, '2015-01-01', '2015-12-31', 3, 5, 'test'
FROM users;
基本上我有两个这样的table:
Table 1: 用户
id_user, name, ...
Table 2: 叶子
id_2, employee (the column employee is the id of the user from the first table), ...
现在 table 两个是空的(没有行),对于第一个 table 的每个用户,我想在第二个用户上创建一行,插入来自首先 table 作为员工列中的值,例如:
Table 2: 叶子
id employee column1 column2 column3 column4 column5
id1 1 date1 date2 integer1 integer2 string
id2 2 date1 date2 integer1 integer2 string
...
我试过的插入:
这个很好用:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description) VALUES (1, '2015-01-01', '2015-12-31', 3, 5, 'test');
这里我试了上面解释的方法,但是不行:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description) VALUES ((SELECT id from users), '2015-01-01', '2015-12-31', 3, 5, 'test');
我收到以下错误:
#1242 - Subquery returns more than 1 row
我确实理解错误:子查询找到了多个结果,因此它无法将值插入员工,我的问题是我不知道作为菜鸟应该使用什么语法。我只想在第二个 table 中为第一个 table 中的每一行创建一行(使用第一个 table 中的 id)。
只需使用insert . . . select
:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
SELECT id, '2015-01-01', '2015-12-31', 3, 5, 'test'
FROM users;