我有一个问题
select name,name_order from name_table where dept_id=XXX;
结果集是
+------------+--------+
| name_order | name |
+------------+--------+
| 0 | One |
| 1 | Two |
| 2 | Three |
| 3 | four |
| 6 | five |
| 9 | six |
+------------+--------+
我必须更新 dept_id 的 name_order,使它们从 0 开始并递增(仅适用于该 dept_id)
注意:name_order 不是索引
,结果应该是这样的
+------------+--------+
| name_order | name |
+------------+--------+
| 0 | One |
| 1 | Two |
| 2 | Three |
| 3 | four |
| 4 | five |
| 5 | six |
+------------+--------+
我尝试了分析函数rowNumber(),它没有帮助
update name_table set name_order = (
ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY name_order)-1
)
where dept_id=XXX order by name_order
在此先感谢-R