1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【MySQL】UPDATEで並び順フィールドをきれいに振りなおす。

Last updated at Posted at 2020-08-25

自分用のメモです。
いつの間にか、並び順の枠がなくなっていて、並び順の管理がしにくくなると思います。
下記のSQLで並び順を維持して番号を振りなおす。

■やり方

# 下記は10間隔で振りなおしている。
# 並び順列「sort」フィールドに新しい番号を入れる。
# orderbyで更新順を更新前のsortの昇順にする。
begin;
set @count:=0;
update hogetable set sort = (@count := @count + 10 ) order by sort;
commit;

割とスマートにできてぱっと見のクエリもわかりやすいです。

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?