I have 1 table which having columns id, name and threat & want to use order by clause for threat column.
Threat 1 is highest priority and 5 is lowest one. Few records won't have threat score and having Null as score.
Hence on displaying it on screen, subtracting the threat score with numeric value 6 (if threat is having value) and same will be used as threat score.
On sorting the column threat by asc, it picking first null values (no threat) and sorting by desc, its picking first value as 5 (lowest threat).
Hence final order on screen is (after subtracting by 6) for ASC. If you see here, threat score with 5 showing as top because SQL sending record sorted by threat score.
+-------------+------------+
| screen data | Table data |
+-------------+------------+
| NULL | Null |
| 1 | 5 |
| 2 | 4 |
| 3 | 3 |
| 4 | 2 |
| 0 | 0 |
+-------------+------------+
Already tried ORDER BY threat * -1 ASC
but its giving error
BigInt unsigned error out of range
Here my requirement is get threat score with 1 as top record while sorting the records because NULL is having precedents over it.
Expected behavior for sort (ASC or desc)
+--------+
| Threat |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 0 |
| NULL |
+--------+
--Working Solution--
With help of awesome people here, below solution worked like charm.
ORDER BY IF (threat=0,6, ifnull(threat,6)) ASC ;