Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BEGIN
- DECLARE loop_done INT DEFAULT 0;
- DECLARE current_user_id INT;
- DECLARE current_todo_id INT;
- DECLARE todo_position INT DEFAULT 0;
- DECLARE cur_users CURSOR FOR
- SELECT id FROM users;
- DECLARE cur_todos CURSOR FOR
- SELECT id FROM todo WHERE user_id = @uid;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET loop_done = 1;
- OPEN cur_users;
- loop_users: LOOP
- FETCH cur_users INTO current_user_id;
- IF loop_done = 1 THEN
- SET loop_done = 0;
- LEAVE loop_users;
- END IF;
- SET @uid = current_user_id;
- SET todo_position = 0;
- OPEN cur_todos;
- loop_todos: LOOP
- FETCH cur_todos INTO current_todo_id;
- IF loop_done = 1 THEN
- SET loop_done = 0;
- SET todo_position = 0;
- LEAVE loop_todos;
- END IF;
- UPDATE todo SET position = todo_position WHERE user_id = @uid;
- SET todo_position = todo_position + 1;
- END LOOP loop_todos;
- CLOSE cur_todos;
- END LOOP loop_users;
- END$$
- +----------+
- | position |
- +----------+
- | 3 |
- | 3 |
- | 3 |
- | 3 |
- | 5 |
- | 5 |
- | 5 |
- | 5 |
- | 5 |
- +----------+
- +----------+
- | position |
- +----------+
- | 0 |
- | 1 |
- | 2 |
- | 3 |
- | 0 |
- | 1 |
- | 2 |
- | 3 |
- | 4 |
- +----------+
Add Comment
Please, Sign In to add comment