使用 UPDATE 改造增量 ID 列
我有一个带有 ID 列的 MySQL 表,旨在从外部数据导入,但对于这种情况,没有提供 ID。因此,我有一列纯零。我需要更新此列以在每行中具有唯一值,并且这些数字没有意义。我可以想到几种方法来做到这一点,但我想知道正确的方法是什么。我的第一个想法类似于 UPDATE table SET id = (SELECT max(id)+1 FROM table) - 但我喜欢可用的优雅解决方案。
编辑:事实证明,运行此查询会触发1093:您无法在 FROM 子句中指定要更新的目标表
- 我想这意味着我确实需要一个更优雅的解决方案,也。
I have a MySQL table with an ID column, intended to be imported from external data, but for this one case there were no IDs provided. Therefore, I have a column of pure zeros. I need to update this column to have unique values in each row, and these numbers have no significance. I can think of several ways to do it, but I'm wondering what the correct way would be. My first idea was something like UPDATE table SET id = (SELECT max(id)+1 FROM table)
- but I like elegant solutions when available.
Edit: It turns out running this query triggers 1093: You can't specify target table for update in FROM clause
- I guess that means I really need a more elegant solution, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能生成一个表视图,在其中添加一个表达式列,该列可以生成一个唯一标识符,就像您所提议的那样,然后导入视图吗?
Can't you generate a View of the table where you add an expression column wich can generate a unique identifier much like you are proposing and then import the view?
我最终这样做了,因为没有任何效果:
I ended up doing this, since nothing was working: