Tuesday, June 5, 2012

Using if condition in order by clause for mysql

We can use the if condition in order by clause. See the following query as an example.

It is sorting the products based on the condition : if sale_price > 0 then sort by sale_price other wise sort by pro_price

SELECT `pro_id`,`pro_price`,`sale_price` FROM `product` order by if(sale_price>0, `sale_price`,`pro_price`) desc

No comments:

Post a Comment