slik
slik

Reputation: 5221

Mysql : Sort by id DESC but also sort by another column and ignore value 0

Im trying to figure out if this is possible.

I want to sort by the id in DESC order, but there are certain records in the table that need to be pushed to the top. I tried some ways, here is one ex. Maybe someone can help me out here?

SELECT *
FROM `table`
ORDER BY CASE WHEN index >0
THEN index
END , id DESC
LIMIT 0 , 30

But I cant seem to get the right output.

Upvotes: 0

Views: 549

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

Almost! Try this:

ORDER BY CASE WHEN index > 0
THEN 0
ELSE 1
END, id DESC

Upvotes: 2

Related Questions