Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- drop table if exists mega_table;
- set @max_rel_return_id = -1;
- set @counter = 0;
- set @last_counter = 0;
- create table mega_table (
- select
- @max_rel_return_id := IF(counter = @counter AND @counter > 0, @max_rel_return_id, 0) as "swap"
- , @counter := counter as "counter"
- , (select returns.reason from returns where returns.counter = @counter and id > @max_rel_return_id order by id asc limit 1) as "reason"
- , @max_rel_return_id := (select returns.id from returns where returns.counter = @counter and id > @max_rel_return_id order by id asc limit 1) as "return_id"
- from (
- select * from things order by counter asc, id asc
- ) as things_ordered
- order by things_ordered.counter desc
- );
- alter table mega_table drop column swap;
- select * from mega_table;
Add Comment
Please, Sign In to add comment