SQL。通过更新修复计数器行

时间:2015-02-18 16:34:39

标签: mysql sql sql-server oracle

也许它是重复的或/并且可能解决方案非常简单,但我找不到任何答案或更好地说要写下正确的搜索查询。

假设我有下表

 ----------------------
| parent | child | pos |
 ----------------------
|   0    |    1  |  0  |
|   0    |    5  |  1  |
|   0    |    3  |  5  |
|   1    |    8  |  0  |
|   1    |    9  |  0  |
|   1    |    7  |  0  |
|   2    |    8  |  3  |
|   2    |    9  |  4  |
 ----------------------

如你所见,pos行有点破碎。例如,对于父0及其子节点,pos值为0,1,5而不是0,1,2。对于父1及其子节点,它们是0,0,0而不是0,1,2

孩子和pos背后绝对没有逻辑。我只是将它们随机化以表明pos不依赖于孩子,但孩子的顺序由其pos值定义。如果之前有订单,它应该是相同的。

现在的问题是:是否有可能使用更新sql语句“修复”这个pos-row,即pos将是不间断且完整的并始终以0开始?

我正在为oracle,mysql和ms sql server寻找一个解决方案或更多解决方案。这是因为我们和他们三个人一起工作。

这就是希望的结果应该是什么样的

 ----------------------
| parent | child | pos |
 ----------------------
|   0    |    1  |  0  |
|   0    |    5  |  1  |
|   0    |    3  |  2  |
|   1    |    8  |  0  |
|   1    |    9  |  1  |
|   1    |    7  |  2  |
|   2    |    8  |  0  |
|   2    |    9  |  1  |
 ----------------------

提前谢谢你。

聚苯乙烯。 A.D. for mysql的解决方案正在为我工​​作

2 个答案:

答案 0 :(得分:0)

我有mysql的这个解决方案

UPDATE your_table
, (
    SELECT parent
    , child
    , @num := IF(@parent = parent, @num + 1, 0) AS rownum
    , @parent := parent
    FROM your_table
    , (SELECT @parent := '', @num := -1) AS dummy
) AS your_table2
SET your_table.pos = your_table2.rownum
WHERE your_table.parent = your_table2.parent
AND your_table.child = your_table2.child

我认为 pos 值的逻辑是数据库的自然顺序。

所以基本上,这个查询为每个断点创建一个新的rownum值。

SELECT parent
, child
, @num := IF(@parent = parent, @num + 1, 0) AS rownum
, @parent := parent
FROM your_table
, (SELECT @parent := '', @num := -1) AS dummy

然后这些值用于更新表

答案 1 :(得分:0)

MS SQL的解决方案

update source_table
set source_table.POS = newposition_table.new_pos
from my_table source_table
inner join (select parent, 
                   child, 
                   ROW_NUMBER() over (partition by parent order by parent, pos) - 1 new_pos
            from my_table) newposition_table
on source_table.child = newposition_table.child