Making it Happen
Making it Happen

Reputation: 47

Combine queries based on number of results from first query

I am trying to combine 2 MySQL queries based on the number of rows returned in the first query, something like this:

mysql_query("SELECT * FROM `new_userprofile_combined`
WHERE `upg_featured` >= NOW() LIMIT 20
IF mysql_num_rows(1st query) < 20
(SELECT * FROM `new_userprofile_combined`
WHERE `upg_featured` < NOW() ORDER BY rand()
LIMIT (20-mysql_num_rows(1st query))) ");

Is this possible? How can I do it?

Upvotes: 0

Views: 49

Answers (1)

Matt Clark
Matt Clark

Reputation: 28619

mysql_num_rows();

Is a PHP function, not an SQL function, so you can not call it in a query, you will have to check the condition after the query, and then run another query.

Upvotes: 1

Related Questions