从 select 创建表,然后通过在 mysql 中添加自动增量列来更改表
正如主题中简要解释的那样,我需要通过选择现有值来创建一个表。 我想要实现的目标是拥有另一列具有自动递增值的列。
这是我已经尝试过的:
CREATE TEMPORARY TABLE temp_tb (
`row_id` bigint(20) NOT NULL AUTO_INCREMENT,
`stm_id` bigint(20) NOT NULL,
descr varchar(20) NOT NULL,
PRIMARY KEY (row_id)
);
然后在选择之后:
INSERT INTO temp_tb (
select stm_id,descr from tb_export
)
我希望在插入时预先填充 row_id 列,但我刚刚收到 sql 语法错误,告诉我列计数与值计数不匹配。
你知道这是否有可能实现吗?
谢谢
as briefly explained in subject, I need to create a table by selecting existing value.
The thing I would like to achieve is to have another column with auto incremented value.
This is what I already tried:
CREATE TEMPORARY TABLE temp_tb (
`row_id` bigint(20) NOT NULL AUTO_INCREMENT,
`stm_id` bigint(20) NOT NULL,
descr varchar(20) NOT NULL,
PRIMARY KEY (row_id)
);
Then after with a select:
INSERT INTO temp_tb (
select stm_id,descr from tb_export
)
I was expecting to have the row_id column prefilled at insert time, but I just got sql syntax error telling me that column count doesn't match value count.
Do you know if this is possible to achieve ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该提供要插入到 temp_tb 中的列的名称:
you should provide the names of the columns you are inserting into your temp_tb:
如果我没记错的话,语法错误与您的插入语法有关。您有一个 temp_tb,它有 3 个字段,全部不为空。您将使用 insert 语句将 2 个字段插入到该表中。 MySQL 参考列出了使用 select 进行插入的语法:
插入 tbl_temp2 (fld_id)
选择 tbl_temp1.fld_order_id
从 tbl_temp1 到 tbl_temp1.fld_order_id > 100;
If I am not mistaken, the syntax error has to do with your insert syntax. You have a temp_tb that has 3 fields, all not null. You are inserting 2 fields into that table with your insert statement. The MySQL ref lists the syntax for insert using select as:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;