如何从 mySql 中的 EXECUTE 语句插入数据?
我的 wp_users
表中有数据,我想将该表中的数据(ID 列除外)复制到另一个名为 wp_users2
的表中。
如果我不关心我想要自动递增的 id 列,我可以这样做:
insert into wp_users2 (select *, NULL as ID from wp_users)
所以我知道我可以通过键入除 ID 之外的所有列标题来做到这一点并手动将其选择为 NULL,
SELECT NULL as id, col2, col3...
但我想动态地执行此操作。我读了这篇很棒的帖子关于如何做到这一点,它有效,但是我不知道如何获取它给我的数据并将其放入插入语句中。
INSERT INTO wp_users2 (
SET @sql = CONCAT('SELECT NULL as ID,',
(SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'ID,', '')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'wp_users'
AND TABLE_SCHEMA = 'wp1'),
' FROM wp_users');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
)
正确的语法是什么?
I have data in a wp_users
table, and I want to duplicate the data from that table (except for the ID column) into another table, called wp_users2
.
If I didn't care about the id
column, which I want to auto-increment, I could just do this:
insert into wp_users2 (select *, NULL as ID from wp_users)
So I know I could do this by typing out all of the column headers except for ID and manually selecting that one as NULL,
SELECT NULL as id, col2, col3...
but I'd like to do it dynamically. I read this great S.O. post about how to do that, and it works, however I can't figure out how to take the data it gives me and put it into an insert statement.
INSERT INTO wp_users2 (
SET @sql = CONCAT('SELECT NULL as ID,',
(SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'ID,', '')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'wp_users'
AND TABLE_SCHEMA = 'wp1'),
' FROM wp_users');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
)
What's the right syntax for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解 - id 是 AUTO_INCRMENT 字段。
因此,尝试使用此脚本作为您的任务的示例 -
As I understand - id is AUTO_INCREMENT field.
So, try to use this script as an example for your task -