Wednesday, April 11, 2012

MySQL "default" result

I have a MySQL table like



id    |  text   |  category   |   active


I select a random line with



SELECT id, text 
FROM table
WHERE category = [category id] AND active = 1
ORDER BY RAND()
LIMIT 1


Some times this would return no results (e.g. if there is no active row in a specific category). What I need to do in that case is to return a "default" row.



My question is: what is the most efficient way to do this? Should I just create an identical table but just with the default rows, which I would query if the above query gives no results? Or should I add the default rows in the same table? And how would you query it?



Note that I am excluding the possibility of generating the default text in PHP, as I want it to be customizable, without having to go and change the code.



Any suggestion is welcome!





No comments:

Post a Comment