gpt4 book ai didi

mysql - 多行插入语句包含相同的选择字段

转载 作者:行者123 更新时间:2023-11-29 13:12:51 25 4
gpt4 key购买 nike

我必须进行多次插入,但所有这些新记录都将具有从其他表之一检索的相同字段:

INSERT INTO `user` (`id`, `name`, `surname`, `retrieved`) VALUES
(null, "A", "A", SELECT id from user_records where id = 1),
(null, "B", "B", SELECT id from user_records where id = 1),
(null, "C", "C", SELECT id from user_records where id = 1)...

如何插入此选择结果而不执行每个记录?

谢谢

最佳答案

您应该为此使用变量:

SET @VAR := SELECT id 
FROM user_records
WHERE id = 1;

INSERT INTO `user` (`id`, `name`, `surname`, `retrieved`)
VALUES
(null, "A", "A", @VAR),
(null, "B", "B", @VAR),
(null, "C", "C", @VAR)...

SELECT id INTO @VAR 
FROM user_records
WHERE id = 1;

INSERT INTO `user` (`id`, `name`, `surname`, `retrieved`)
VALUES
(null, "A", "A", @VAR),
(null, "B", "B", @VAR),
(null, "C", "C", @VAR)...

关于mysql - 多行插入语句包含相同的选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855166/

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