tags:

views:

64

answers:

1

I currently have the following Product table:

Product
_______
id, INT
name, String
featured, ENUM(Y, N)

If there is a featured product in the above, I will return that item. If none of the above are featured, I will return anyone of them. I currently have the following query which is functional:

SELECT *
FROM Product
WHERE (featured = 'Y' OR featured = 'N')
LIMIT 1

Does anyone have any objection with the above SQL, it kind of feels wrong? If so, what would be the best way to do it? The above is the simplest case of a much larger query. I could have used ORDER BY featured but it doesn't do anything in pushing the featured product to the top of my list.

+5  A: 

This should do it. You force the 'Y' value(s) to the top of the list.

SELECT *
FROM Product
ORDER BY CASE WHEN featured = 'Y' THEN 0 ELSE 1 END
LIMIT 1
David M