Purpose:
Every half-hour I query database to find a post that havn’t published to publish,every time I query by different category,if no posts in this category,switch to next category.
The if check done by SQL
pseudo-code
SELECT FROM post WHERE post.cat_ID='7' AND post_date > '$last_query_time_cat_ID_7'
IF NO_POSTS_MATCH SELECT FROM post WHERE post.cat_ID='8' AND post_date>'$last_query_time_cat_ID_8'
...
pseudo explain:select one post of one category by its last query time of this category,if no new post of this category select from another category by its last query time.
My question:
Is there such SQL to do this work?
This can be done by php to check if no posts return,then make another sql query.This could make many queries.
Somebody show me the sql-pseudo-code of this problem?
Could look like this:
Major points
First get the timestamp of the last query per category in a subquery. We are only interested in
cat_ID >= 7Join this to the base table and filter only rows with a later
post_datethanlast_querypercat_ID.Sort it by
cat_ID– you seem to want smallestcat_IDfirst. Secondary sort bypost_datedescending, so the last post comes first.If there are none in
cat_ID7,cat_ID8 will be first etc. Voila.