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?
Any suggestion is welcome!
EDIT
A few updates to the question:
-
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.
-
There will be a default row per category
What is the default value you want? The 1st id in the table?
If you wish for a certain default value, add a ENUM column called
default('TRUE', 'FALSE'):Easier one:
Explanation:
Basicly this query fetches all the rows you count on to get, and combine it with a default row. They got a prio value each, which you order them in before doing
RAND(). The thing remaining is how you wish to store your default rows.