there is such a structure

id|date 1 |2015-05-26 2 |2015-05-27 ............. 

I can not figure out how to sort by date so that it displays the current day at the top of the list, and then the rest of the records

  • Do you have dates and more and less current? - Batanichek
  • @Batanichek, yes. and this is the problem. dates can be different. Though 2026))) - Vfvtnjd

2 answers 2

 select * from table order by if(`date`=current_date(),0,1), `date` 
  • I will try, I will write result. - Vfvtnjd
  • very interesting you wrote - Vfvtnjd

I think you can do it through union all

for example who so

 select id,date from table where date=Current_date() union all select id,date from table where date<>Current_date() 

Update

With comments @Mike

You can make this option

 select id,date from ( select id,date, 1 as ord from table where date=Current_date() union all select id,date,2 as ord from table where date<>Current_date() or date is null) order by ord 

But the @Mike option is more beautiful

  • so obviously not. The order of the records without specifying order by is not defined. and the order will act on the whole expression - Mike
  • @Mike I didn’t understand much, why date = Current_date () is not defined first, then everything else .. or am I misunderstanding something? - Batanichek
  • 2
    The optimizer having seen yours = and <> can easily optimize the request for one sample from the entire table by removing the union. The order of the output records does not have to match the order of the union samples. the optimizer executes queries in the order it deems necessary. There is no explicitly specified order by for the entire SQL query to guarantee no order. - Mike
  • And your request loses records for which date is NULL - Mike
  • @Mike did not know about the order. so in all sql or only in my? - Batanichek