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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:34:08+00:00 2026-05-20T00:34:08+00:00

I am querying a table for a set of data that may or may

  • 0

I am querying a table for a set of data that may or may not have enough results for the correct operation of my page.

Typically, I would just broaden the range of my SELECT statement to insure enough results, but in this particular case, we need to start with as small of a range of results as possible before expanding (if we need to).

My goal, therefore, is to create a query that will search the db, determine if it got a sufficient amount of rows, and continue searching if it didn’t. In PHP something like this would be very simple, but I can’t figure out how to dynamically add select statements in a query based off of a current count of rows.

Here is a crude illustration of what I’d like to do – in a SINGLE query:

SELECT *, COUNT(`id`) FROM `blogs` WHERE `date` IS BETWEEN '2011-01-01' AND '2011-01-02' LIMIT 25

IF COUNT(`id`) < 25 {
    SELECT * FROM `blogs` WHERE `date` IS BETWEEN '2011-01-02' AND '2011-01-03' LIMIT 25
}

Is this possible to do 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-20T00:34:09+00:00Added an answer on May 20, 2026 at 12:34 am

    You have 2 possible solutions:

    1. Compare the count on the programming language side. And if there is not enough – perform one more query. (it is not as bad as you think: query cache, memcached, proper indexes, enough memory on server, etc – there are a lot of possibilities to improve performance)
    2. Create stored procedure
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data I am querying. The table is composed of two columns
I have a set of data that's organized hierarchically that should be able to
Querying a Nested Set Model table , here's the SQL... how can this be
I have a table containing hierarchical data. There are currently ~8 levels in this
I'm implementing a custom data provider, I have gotten it to the point that
I have a 5 year data set partitioned into quarterly tables. I also have
I have a table code_prices that looks something like this: CODE | DATE |
I'm trying to wrap my head around querying a table that has hierarchical category
I have set of components that i wish to let the users download from
I have a project with a number of different classes querying and modifying data

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.