问题
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