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

  • SEARCH
  • Home
  • 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 610909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:43:36+00:00 2026-05-13T17:43:36+00:00

I need a single query that is similar to SELECT datetime FROM blog WHERE

  • 0

I need a single query that is similar to

SELECT datetime FROM blog WHERE UNIX_TIMESTAMP(datetime) < '".($date_string_in_seconds +1000)."' ORDER BY datetime DESC

but instead of returning the latest date that is smaller than the one given in parameter, it has to return:
-the record with the exact date that is given in the parameter
-if exists, one with the earliest timestamp that is higher than the parameter
-if exists, one with the latest timestamp that smaller than the parameter (what the query above returns)
In other words, it should return the given date and the ones right after and before that. The query should return at most three and at least one record with a valid date.

I need this information as a basis for enabling/disabling prev and next entry buttons on a blog page and currently get the information with two separate queries.
I suspect there is a better way. Is this possible with a single query?

  • 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-13T17:43:36+00:00Added an answer on May 13, 2026 at 5:43 pm
    SELECT  datetime
    FROM    (
            SELECT  datetime
            FROM    blog
            WHERE   datetime < FROM_UNIXTIME($date)
            ORDER BY
                    datetime DESC
            LIMIT 1
            ) p
    UNION ALL
    SELECT  datetime
    FROM    blog
    WHERE   datetime = FROM_UNIXTIME($date)
    UNION ALL
    SELECT  datetime
    FROM    (
            SELECT  datetime
            FROM    blog
            WHERE   datetime > FROM_UNIXTIME($date)
            ORDER BY
                    datetime
            LIMIT 1
            ) n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trip that has many residencies. I need a single query that
How to return a select result with batches through a single query, that should
In SP2007/MOSS I need to run a CAML query against a single list like
I need to have a single instance application (as per this answer ), but
I need to create a single trace file that spans several days for one
I need to develop a single routine that will be fired each 5 minutes
I need to create a Linq query that would have the following logic: IEnumerable<string>
I need help writing a T-SQL query that will generate 52 rows of data
I have a database that I need to query over and over as fast
Currently when I need to run a query that will be used w/ paging

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.