28 November 2004

Sort by specific ID order

Recently I needed to return some ids… in a certain order, and I realize I have never done that … after some digging behold the ORDER BY FIELD

MySQL never ceases to amaze me… and this may magnify lack of grasp on technical stuff but here goes..

Recently I needed to return some ids’ in a certain order, and I realize I have never done that … after some digging behold the “ORDER BY FIELD”

SELECT id 
FROM some_table
WHERE id in (13,5,20,7)
ORDER BY FIELD(id,5,20,13,7);

Ofcourse there are always more than one way to skin a cat … so I present the “FIND IN SET”.

SELECT id 
FROM some_table
WHERE id in (13,5,20,7)
ORDER by FIND_IN_SET(id,"5,20,13,7");

Cool !

 

Comment



note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile | twitter

recently :::