gpt4 book ai didi

mysql - 为另一个表中的每个 ID 在表中插入行

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:32 25 4
gpt4 key购买 nike

基本上我有两个这样的表:

表 1:用户

id_user, name, ...

表 2:叶子

id_2, employee (the column employee is the id of the user from the first table), ...

现在,第二个表是空的(没有行),对于第一个表中的每个用户,我想在第二个表中创建一行,将第一个表中的 ID 作为值插入员工列中,喜欢:

表 2:叶子

id    employee    column1    column2    column3    column4    column5   
id1 1 date1 date2 integer1 integer2 string
id2 2 date1 date2 integer1 integer2 string
...

我试过的插入:

  1. 这个很好用:

    INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
    VALUES (1, '2015-01-01', '2015-12-31', 3, 5, 'test');
  2. 这里我尝试了上面解释的方法,但它不起作用:

    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

我确实理解错误:子查询找到多个结果,因此它无法将值插入员工,我的问题是我不知道应该使用什么语法,因为我是菜鸟。我只想在第二个表中为第一个表中的每一行创建一行(使用第一个表中的 ID)。

最佳答案

只需使用 insert 。 . .选择:

INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
SELECT id, '2015-01-01', '2015-12-31', 3, 5, 'test'
FROM users;

关于mysql - 为另一个表中的每个 ID 在表中插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32971010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com