我有一個查詢我們可以用行數替換列的所有值嗎?
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
這會更接近OP的問題:http://www.sqlfiddle.com/#!4/f02c1/2 – Passerby 2013-05-09 05:45:15
@Passerby是的,我沒有不考慮dept_id.I更新我的答案。 – Gentlezerg 2013-05-09 05:58:30