in reply to [OT] MySQL recalibrating a sort-index
DROP TABLE IF EXISTS t_tree_insert, _t; CREATE TABLE `t_tree_insert` ( `f_node_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `f_parent_id` INT(10) UNSIGNED NULL DEFAULT '0', `f_name` VARCHAR(200) NOT NULL, `f_sort` FLOAT NULL DEFAULT NULL, PRIMARY KEY (`f_node_id`), UNIQUE INDEX `sort` (`f_parent_id`, `f_sort`), INDEX `f_parent_id` (`f_parent_id`), FULLTEXT INDEX `f_name` (`f_name`) ) COMMENT='test tree move operations' COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=0 ; insert into t_tree_insert values (130, 1, 'other array', 1) , (142, 2, 'B06', 1) , (143, 2, 'B2L', 2) , (144, 2, 'B2M', 3) , (200, 2, 'INSERT', 3.5) , (145, 2, 'B2N', 4) , (146, 2, 'B2O', 5) , (150, 3, 'other array', 1) ; select * from t_tree_insert order by f_parent_id, f_sort; set @sort=0; create temporary table _t as select f_node_id, f_parent_id, f_name, (@sort:=@sort+1) as f_sort from t_tree_insert as T where f_parent_id = 2 order by T.f_sort ; select * from _t; replace into t_tree_insert (f_node_id, f_parent_id, f_name, f_sort) select * from _t ; DROP TABLE _t; select * from t_tree_insert order by f_parent_id, f_sort;
| 130 | 1 | other array | 1 |
| 142 | 2 | B06 | 1 |
| 143 | 2 | B2L | 2 |
| 144 | 2 | B2M | 3 |
| 200 | 2 | INSERT | 3,5 |
| 145 | 2 | B2N | 4 |
| 146 | 2 | B2O | 5 |
| 150 | 3 | other array | 1 |
| 142 | 2 | B06 | 1 |
| 143 | 2 | B2L | 2 |
| 144 | 2 | B2M | 3 |
| 200 | 2 | INSERT | 4 |
| 145 | 2 | B2N | 5 |
| 146 | 2 | B2O | 6 |
| 130 | 1 | other array | 1 |
| 142 | 2 | B06 | 1 |
| 143 | 2 | B2L | 2 |
| 144 | 2 | B2M | 3 |
| 200 | 2 | INSERT | 4 |
| 145 | 2 | B2N | 5 |
| 146 | 2 | B2O | 6 |
| 150 | 3 | other array | 1 |
probably I can even do it without a temp table...
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: [OT] MySQL recalibrating a sort-index
by LanX (Saint) on Feb 17, 2018 at 00:30 UTC |