A specific value(only one) to be first in a query result. Rest order by time stamp

て烟熏妆下的殇ゞ 提交于 2020-01-06 03:11:13

问题


I have a search result query for posts. And I want posts written by myself(userId = 27) to be first in query result and rest ordered by time stamp. Can anyone give me query for that in mysql?


回答1:


select *
from posts
order by 
  if (userid=27, -1, any_timestamp_include_zero);

include your full table schema help much better




回答2:


How about something like:

select * from post
order by 
    case 
        when userid = 25 then '0001-01-01 00:00:00' 
        else my_timestamp
    end

(formatting the '0001-01-01' part appropriately for MySql)




回答3:


Something simple like this:

SELECT * FROM POST WHERE userId = 25
UNION
SELECT * FROM POST WHERE userId <> 25 ORDER BY TIMESTAMP_FIELD

Could work for your need?



来源:https://stackoverflow.com/questions/4570872/a-specific-valueonly-one-to-be-first-in-a-query-result-rest-order-by-time-sta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!