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 4537766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:43:10+00:00 2026-05-21T14:43:10+00:00

I have to get all entries in database that have a publish_date between two

  • 0

I have to get all entries in database that have a publish_date between two dates. All dates are stored as integers because dates are in UNIX TIMESTAMP format…
Following query works perfect but it takes “too long”. It returns all entries made between 10 and 20 dazs ago.

SELECT * FROM tbl_post WHERE published <  (UNIX_TIMESTAMP(NOW())-864000) 
AND published> (UNIX_TIMESTAMP(NOW())-1728000)

Is there any way to optimize this query? If I am not mistaken it is calling the NOW() and UNIX_TIMESTAMP on evey entry. I thought that saving the result of these 2 repeating functions into mysql @var make the comparison much faster but it didn’t. 2nd code I run was:

SET @TenDaysAgo = UNIX_TIMESTAMP(NOW())-864000;
SET @TwentyDaysAgo = UNIX_TIMESTAMP(NOW())-1728000;
SELECT * FROM tbl_post WHERE fecha_publicado <  @TenDaysAgo 
AND fecha_publicado > @TwentyDaysAgo;

Another confusing thing was that PHP can’t run the bove query throught mysql_query(); ?!

Please, if you have any comments on this problem it will be more than welcome 🙂

Luka

  • 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-21T14:43:11+00:00Added an answer on May 21, 2026 at 2:43 pm

    PHP’s mysql_query function (assuming that’s what you’re using) can only accept one query per string, so it can’t execute the three queries that you have in your second query.

    I’d suggest moving that stuff into a stored procedure and calling that from PHP instead.

    As for the optimization, setting those variables is about as optimized as you’re going to get for your query. You need to make the comparison for every row, and setting a variable provides the quickest access time to the lower and upper bounds.

    One improvement in the indexing of the table, rather than the query itself would be to cluster the index around fecha_publicado to allow MySQL to intelligently handle the query for that range of values. You could do this easily by setting fecha_publicado as PRIMARY KEY of the table.

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

Sidebar

Related Questions

I have a requirement that i want to get all the system services running
I want to get all the Applications that have intentlisteners to Intent.CATEGORY_HOME so basically
I have entries in a mysql database that have htmlentities, so, e.g., &ouml instead
I have a database that looks like this: (dates are formated with strtotime )
I have two tables in a derby database that I want to query together.
I have a query to get all the compatible phones in a specific country,
I need get all items these have no categories int? categoryId = null; var
I have this issue trying to get all the text nodes in an HTML
I have to create an SQL Query to get all rows starting with a
I have a written a method to get all the records and return in

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.