Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:12:37+00:00 2026-05-26T21:12:37+00:00

Purpose : Every half-hour I query database to find a post that havn’t published

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:12:38+00:00Added an answer on May 26, 2026 at 9:12 pm

    Could look like this:

    SELECT p.post_ID, p.cat_ID, p.post_date
    FROM   post p
    JOIN   (
        SELECT cat_ID
              ,max(query_time) AS last_query
        FROM   post 
        WHERE  cat_ID >= 7
        GROUP  BY 1
        ORDER  BY 1) l USING (cat_ID)
    -- in some RDBMS you need ON l.cat_ID = p.cat_ID in the JOIN clause
    WHERE  p.post_date > l.last_query
    ORDER  BY p.cat_ID, p.post_date DESC
    LIMIT  1;  -- In T-SQL you need to write "SELECT TOP 1" instead
    

    Major points

    • First get the timestamp of the last query per category in a subquery. We are only interested in cat_ID >= 7

    • Join this to the base table and filter only rows with a later post_date than last_query per cat_ID.

    • Sort it by cat_ID – you seem to want smallest cat_ID first. Secondary sort by post_date descending, so the last post comes first.

    • If there are none in cat_ID 7, cat_ID 8 will be first etc. Voila.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to configure my computer so that say every half an hour it
It is my opinion that every language was created for a specific purpose. What
Again i see that the @PostConstruct is firing every time even though no binding
Purpose of the app: A simple app that draws a circle for every touch
I have a mysql trigger that logs every time a specific table is updated.
Every time anyone mentions testing against undefined , it's pointed out that undefined is
For the purpose of organizing streamlined database timer-based polling, with every updating of database
I have a variable formvar that is incremented every time a user adds an
Some people say that every programming language has its complexity budget which it can
Hello EVery I am new to SQl. query to result in the following records.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.