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 :::

diversions :::

45+ Amazing Insect Shots in Photography
Insects are one of the most fascinating creatures on earth. There are more than 800, 000 species of insects in the world.
Grayscale color | Stroep Blog
This is how I create a grayscale color in actionscript 3.
Google Flash API
This is great ... google has made this easy ... stay tuned to see what i am working on ...
25 Free Mac Apps That Will Boost Your Productivity
There are many applications that can help you work faster and efficiently. Though, not many applications come cheap.
you still want more »