Select query but show the result from record number 3

前端 未结 1 1156
北海茫月
北海茫月 2021-01-25 21:34

I have some simple query:

SELECT * FROM table 

You all know the result:

|id|   foo   |    bar   |
-------------------------
|1          


        
相关标签:
1条回答
  • 2021-01-25 21:45

    This way you get id = 3 first:

    SELECT *
    FROM tbl
    ORDER BY (id = 3) DESC
    -- ,id
    

    Order by id additionally if you want the rest ordered, too.

    Explanation:

    The expression evaluates to boolean. FALSE (= 0 in mysql) sorts before TRUE (= 1 in mysql), so we order descending.

    It also automatically covers the case of id being NULL. I quote the manual again here:

    When doing an ORDER BY, NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.

    0 讨论(0)
提交回复
热议问题