将整数列添加到基于现有列的现有 mysql 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8537990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add integer column to an existing mysql table based on existing column
提问by sckott
I want to add a new column to an existing mysql table, where the new column is a unique integer number for each unique value of an existing column in the table. For example, if the existing column has unique values A and B (and there might be 50 rows of each of A and B), then add a new column with values of 1 and 2 in each row where there is an A and B, respectively.
我想在现有的 mysql 表中添加一个新列,其中新列是表中现有列的每个唯一值的唯一整数。例如,如果现有列具有唯一值 A 和 B(并且 A 和 B 各有 50 行),则在有 A 和 B 的每一行中添加值为 1 和 2 的新列,分别。
回答by Anderson Carniel
well, it require two commands
好吧,它需要两个命令
ALTER TABLE your_table ADD COLUMN your_column INTEGER UNIQUE;
And then for each column record, you create an update statements to do so, like:
然后对于每个列记录,您创建一个更新语句来执行此操作,例如:
UPDATE your_table SET your_column = 1 WHERE column = 'A'
UPDATE your_table SET your_column = 2 WHERE column = 'B'
Or you create a procedure for that, if the records are many.
或者您为此创建一个过程,如果记录很多。