重新索引 mysql 中的位置列
这是一个 MySQL 问题:
我有一个具有以下结构的表。
reference
position
parent
每个项目都有一个位置,每个位置只能使用一次。位置应递增(因此,如果有 40 个项目,则位置应从 1 到 40)。
然而,该表中的数据到处都是(同一位置被多次使用,有些行没有位置)。我想通过重新索引位置来重置表格。在执行此操作时,我想尊重现有订单(即使它们并不完美)(如果可能的话,否则可以放弃头寸)。
------------------------------------------------------
| reference | position | parent |
|-----------------|--------------------|--------------|
|ASHDFNS |2 |89 |
|BSHDFNS |2 |89 |
|CSHDFNS |1 |89 |
|DSHDFNS |100 |89 |
|ESHDFNS |8 |89 |
|FSHDFNS |22 |89 |
|ASHDFNS |1 |11 |
|BSHDFNS |22 |11 |
|CSHDFNS |333 |11 |
|-----------------|--------------------|--------------|
所需的
-------------------------------------------------------
| reference | position | parent |
|-----------------|--------------------|--------------|
|CSHDFNS |1 |89 |
|ASHDFNS |2 |89 |
|BSHDFNS |3 |89 |
|ESHDFNS |4 |89 |
|FSHDFNS |5 |89 |
|DSHDFNS |6 |89 |
|ASHDFNS |1 |11 |
|BSHDFNS |2 |11 |
|CSHDFNS |3 |11 |
|-----------------|--------------------|--------------|
编辑:抱歉,简单地自动递增位置列将不起作用,因为该表描述了多个父项中项目的位置(并且项目可以有多个父项)
This is a MySQL question:
I have a table with the following structure.
reference
position
parent
Every item is meant to have a position, and each position should only be used once. The positions should increment (so if there are 40 items then the positions should go from 1-40).
However the data in this table is all over the place (the same position being used more than once, some rows without a position). I'd like to reset the table by re-indexing the positions. I'd like to respect the existing orders (even if they are not perfect) when doing this (if at all possible - if not the positions can be discarded).
------------------------------------------------------
| reference | position | parent |
|-----------------|--------------------|--------------|
|ASHDFNS |2 |89 |
|BSHDFNS |2 |89 |
|CSHDFNS |1 |89 |
|DSHDFNS |100 |89 |
|ESHDFNS |8 |89 |
|FSHDFNS |22 |89 |
|ASHDFNS |1 |11 |
|BSHDFNS |22 |11 |
|CSHDFNS |333 |11 |
|-----------------|--------------------|--------------|
Desired
-------------------------------------------------------
| reference | position | parent |
|-----------------|--------------------|--------------|
|CSHDFNS |1 |89 |
|ASHDFNS |2 |89 |
|BSHDFNS |3 |89 |
|ESHDFNS |4 |89 |
|FSHDFNS |5 |89 |
|DSHDFNS |6 |89 |
|ASHDFNS |1 |11 |
|BSHDFNS |2 |11 |
|CSHDFNS |3 |11 |
|-----------------|--------------------|--------------|
EDIT: Sorry, simply auto increment the position column won't work as the table describes positions of items within multiple parents (and items can have more than one parent)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将列位置设置为 auto_increment,
创建一个具有类似架构的表,但在从 old_table 顺序插入之后
之后按您想要的任何顺序重命名 old_table,将 new_table 重命名为 old_table
否则,定义一个用户变量来表示位置,
如下所示 - mysql中更新并选择同一个表问题
或此 - 更新列,使其包含行位置
create a table with similar schema, but make the column position as auto_increment
after that insert from old_table order by whatever order you desire
after that, rename old_table, rename new_table to old_table
Otherwise, define a user variables to represent the position
like this - update and select the same table problem in mysql
or this - Updating column so that it contains the row position