有时,我遇到了一种我想用简单的脚本迁移数据库更改的情况。比如说,我插入了(INPUT)值,例如
INSERT into mytable (CodeId, ValueData, category) VALUES (3, 4, 'COMPUTERS');
INSERT into mytable (CodeId, ValueData, category) VALUES (3, 12, 'TABLES');
如何使用select语句生成插入语句,以便运行如下的选择查询:
SELECT (super query) from mytable where CodeId = 3
并将其作为输出:
INSERT into mytable (CodeId, ValueData, category) VALUES (3, 4, 'COMPUTERS'), (3, 12, 'TABLES') ;
修改
我目前正在尝试这样的事情:
select concat('insert into mytable (CodeId, ValueData, category)
VALUES (', select (CodeId, ValueData, category) as gstr from mytable, ')')
我认为它应该是那条线......
答案 0 :(得分:2)
完全未经测试,但应该让你走上正确的轨道......
SELECT Concat('INSERT INTO mytable (CodeId, ValueData, category) VALUES', Group_Concat(the_values)) As result
FROM (
SELECT Concat('(', CodeId, ', ', ValueData, ', ', category, ')') As the_values
FROM mytable
WHERE CodeId = 3
) As x
答案 1 :(得分:1)
试试这个,
Select CodeId, ValueData, category
into DESTTableName
from mytable where CodeId=3;
答案 2 :(得分:0)
此类插入的正确语法是:
INSERT INTO MyTable(CodeId, ValueData, category)
SELECT CodeId, ValueData, category
FROM MYTable
WHERE CodeID=3
您的示例是否在同一个表中选择并插入数据?
只要所选列与您要输入的列的模式匹配,就可以了。