Created
March 8, 2012 11:00
-
-
Save haruta/2000437 to your computer and use it in GitHub Desktop.
有る親IDに対する連番をどうにかしたいSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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