Users Table:
ID|Name|Deleted
1 | | N
2 | b | Y
3 | c | N
4 | d | N
Deleted default value is 'N'
I want to order by id desc but I want to display deleted user last.
i.e. when Deleted 'Y' should appear last
You can use multiple keys in order by. So:
order by (deleted = 'Y') asc, id desc
The expression (deleted = 'Y') is a boolean expression that MySQL treats as a number with 1 for true and 0 for false. The asc puts the true values last.
In most other databases, you would do this with a case:
order by (case when deleted = 'Y' then 1 else 2 end) desc, id desc
ORDER BY deleted, id DESC (or ORDER BY deleted ASC, id DESC) since the values N/Y are alphabetic order?(deleted = 'Y') version also works well any ANSI SQL compliant dbms that supports boolean. If deleted = 'Y', then TRUE is returned, else FALSE. And FALSE is ordered before TRUE.