Hello
Let's say I retrieve the most viewed productIDs with this query:
Code:
SELECT productid, COUNT(productid) AS views FROM products WHERE products.merchantcategory = 'categoryname') ORDER BY views DESC LIMIT 3
then I'll check with php the number of returned rows, if this number is 0 I'll run the query again but replacing this category with its parent:
Code:
SELECT productid, COUNT(productid) AS views FROM products WHERE products.merchantcategory = 'parentcategoryname' ORDER BY views DESC LIMIT 3
I suppose this can be done in a smarter way within the query, but I don't understand why
I tried searching for IF condition samples but honestly I can't get them to work (
MySQL :: MySQL 5.0 Reference Manual :: 11.4 Control Flow Functions )
basically I'd like to run a query like this:
Code:
IF (query1 returns 0 rows) { execute euery2 and return its results} else { return query1 results }
any suggestions?