I have a database table that has two fields , date and name.
I want to have my query pull the first 20 by newest date first, then the rest of the query to pull the other elements by name alphabetically.
So that way the top 20 newest products would show first, then the rest would be ordered by name.
It’s a bit ugly, but you can do it in one query:
The innermost query,
dummy, initializes our@rankvariable. The next derived table,dateranked, ranks all rows by recency (breaking ties byname). The outermost query then simply re-orders the rows by our computedrank, treating ranks greater than 20 as rank #21, and then byname.UPDATE: This query version is more compact, puts the conditional ranking logic in the outermost ORDER BY, uses IF() rather than CASE/END.