Show first selected mysql row

For example:

id 1,2,3,4,5,6,7,8,9,10

I want to show first id = 7and after 10,9,8,6,5,4,3,2,1. How to write this code using php? Please help me(Sorry for bad English)

+4
source share
2 answers

MySQL is convenient when logical expressions are treated as integers, and for "1" - "1" and "0" - false. A quick way to write this:

order by (id = 7) desc, id desc

In other databases you need to use caseor similar logic.

+4
source

You can order a specific expression case:

SELECT   *
FROM     mytable
ORDER BY CASE id WHEN 7 THEN 0 ELSE 1 END ASC, id DESC
+1
source

Source: https://habr.com/ru/post/1620872/


All Articles