To start things off, I want to make it clear that I’m not trying to order by descending order.
I am looking to order by something else, but then filter further by displaying things in a second column only if the value in that column 1 row below it is less than itself. Once It finds that the next column is lower, it stops.
Example:
Ordered by column-------------------Descending Column
353215 20
535325 15
523532 10
666464 30
473460 20
If given that data, I would like it to only return 20, 15 and 10. Because now that 30 is higher than 10, we don’t care about what’s below it.
I’ve looked everywhere and can’t find a solution.
EDIT: removed the big number init, and edd the counter in ifnull test, so it works in pure MySQL:
ifnull(@prec,counter)and notifnull(@prec,999999).If your starting table is t1 and the base request was:
Then with a mysql variable you can do the job:
except
here I need the 99999 as a max value for your columnand there’s maybe a way to put the initialisation of@precto NULL somewhere in the 1st request.Here the prec column contains the 1st row value counter, and then the counter value of each row if it less than the one from previous row, and -1 when this becomes false.
Update
The outer select can be removed completely if the variable assignment is done in the
WHEREclause:regilero EDIT:
I can remove the 1st initialization query using a temporary table (left join) of 1 row this way: but this may slow down the query, maybe.
As said by @Mike using a simple UNION query or even :
is better if you want to avoid the first query.
So at the end the nicest solution is:
EXPLAIN SELECT output: