Skip to content

Instantly share code, notes, and snippets.

@haruta
Created March 8, 2012 11:00
Show Gist options
  • Save haruta/2000437 to your computer and use it in GitHub Desktop.
Save haruta/2000437 to your computer and use it in GitHub Desktop.
有る親IDに対する連番をどうにかしたいSQL
create table t
(
id int auto_increment not null primary key,
parent_id int,
in_seq_num int
);
// parent_id 内の連番を in_seq_num に入れたい
set @lastid := 0;
set @number := 0;
update t set in_seq_num =
IF(@lastid < t.parent_id && @last_id := t.parent_id, @number := 1, @number := @number + 1)
ORDER BY parent_id, id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment