Tag-Archive for ◊ Order By ◊

Author: ravi
• Thursday, November 20th, 2008

I needed to sort results from a MySQL table in the order in which I put them in the in clause. After googling a lot, I found the following

SELECT * FROM PRODUCTS WHERE PRODID IN (3, 6, 1) ORDER BY FIELD(PRODID , 3, 6, 1)

This shows rows with PRODID 3, 6 and 1in same order.

You can sort in the reverse order as well

SELECT * FROM PRODUCTS WHERE PRODID IN (3, 6, 1) ORDER BY FIELD(PRODID , 3, 6, 1) DESC

Category: Tech  | Tags: ,  | Leave a Comment