How to put row on top in mysql query.

后端 未结 4 1610
旧巷少年郎
旧巷少年郎 2021-02-14 12:33

Hi i have 100 records in my SQL table i want to sort them ASC by name but i need one record on top of all record nr 43.

Is there way i can pull this record 43 first and

4条回答
  •  广开言路
    2021-02-14 12:46

    This query should add a column called priority, which has the value 1 on the record with id 43 and 0 on all others. Then you sort by priority first.

    SELECT mytable.*, IF(id = 43, 1, 0) AS priority FROM mytable ORDER BY priority DESC, name ASC
    

提交回复
热议问题