MySQL如何通过自动增加列来更新表

编程入门 行业动态 更新时间:2024-10-25 18:24:27
本文介绍了MySQL如何通过自动增加列来更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

具有10万行的MySql 5.3表具有一个主键.

A MySql 5.3 table with 100K rows has a primary key.

还有一个整数列,它不是键的一部分.我想更新此列以包含表的唯一编号.例如.对于第一个记录,它应包含1,对于第二个记录,应包含2,依此类推.

There is also an integer column which is not part of the key. I would like to update this column to contain a unique number for the table. E.g. for the first record it should contain 1, for the second 2 etc.

这也可以是自动递增的列,但是MySql不允许在非关键列上自动递增.我不希望此列成为键的一部分,因为它是从文件等中填充的方式.

This could as well be an auto-increment column, but MySql does not allow auto-increment on non-key columns. I don't want this column to be part of the key, because of the way it gets populated from a file etc.

那么这样的查询看起来如何?

So how such a query would look like?

推荐答案

我不知道您为什么要这样做,但是可能的解决方法是:

I don't know why do you want to do something like this, but a possible solution is this:

set @rownum:=0; update <table> set column = @rownum:=rownum+1 order by <field>

更多推荐

MySQL如何通过自动增加列来更新表

本文发布于:2023-10-22 22:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!