Part order by in mysql -


id |  datetime 

how can order id, bump recent 2 dated rows positions 1 , 5?

i cannot add sort column.

is possible sql or need array sorting in php?

-------edit

id | datetime 1    2000-01-01 00:00:00 2    2000-01-01 00:00:10 3    2000-01-01 00:00:02 4    2000-01-01 00:00:09 5    2000-01-01 00:00:20 6    2000-01-01 00:00:05 

i expect out latest 2:

ids:  5,2 

then rest ordered via id, should like:

ids: 5,2,1,3,4,6 

in case suggest sort on php side. yes possible create mysql query return order need, not powerful performance perspective

http://sqlfiddle.com/#!9/66062/1

select t.* table1 t left join (   select if(@idx null,@idx:=2,@idx:=1) idx, id   table1   order `datetime` desc   limit 2   ) t1 on t.id = t1.id order t1.idx desc, t.id 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -