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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:28:59+00:00 2026-05-26T17:28:59+00:00

Due to a need to join multiple tables in a single query, I am

  • 0

Due to a need to join multiple tables in a single query, I am not able to use SQL_CALC_FOUND_ROWS (or LIMIT) on the outer query (the number would be higher than expected).

Here is the ideal query, if MySQL supported using SQL_CALC_FOUND_ROWS within subqueries:

SELECT events.*, bands.*
    FROM events
        LEFT JOIN bands ON events.event_id = bands.event_id
    WHERE e.event_id IN (
            SELECT * FROM (
                SELECT SQL_CALC_FOUND_ROWS e.event_id
                    FROM events
                    WHERE events.date > NOW()
                    ORDER BY events.date ASC LIMIT 0, 25
            ) ALIAS
    )
ORDER BY events.date ASC

This question suggests creating an aliased table, but I am not sure if I can do this with PDO prepared statements.

I attempted this, but got a parse error because FOUND_EVENTS was interpreted as being a (nonexistent) column name:

$st1 = $pdo->prepare("(SELECT SQL_CALC_FOUND_ROWS e.event_id
                            FROM events
                            WHERE events.date > NOW()
                            ORDER BY events.date ASC LIMIT 0, 25
                      ) AS FOUND_EVENTS");

$st2 = $pdo->prepare("SELECT events.*, bands.*
                       FROM events
                           LEFT JOIN bands ON events.event_id = bands.event_id
                       WHERE e.event_id IN (
                           FOUND_EVENTS
                       )
                       ORDER BY events.date ASC");

$st1->execute();
$st2->execute();

I would prefer to stick to prepared statements if possible, but any working solution would be great!

  • 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-26T17:29:00+00:00Added an answer on May 26, 2026 at 5:29 pm

    I’d do it in two separate queries.

    If your reason for using SQL_CALC_FOUND_ROWS is to try and save execution time, you may be surprised to know that in many cases, it will be much, much faster, and less load on the database, to use two separate queries instead, one of them with a simple COUNT(*) grouping function to calculate the total rows, and the other one with the LIMIT in place to actually retrieve the rows.

    Here is more explanation of why this is the case.

    What’s more, if you do it in two separate queries, then you can do the one that actually fetches the rows first and then you only need to do the second one with the COUNT(*) if the first one returned the maximum number of rows.

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

Sidebar

Related Questions

I need a reliable messaging framework that runs over http/https (due to client security
Due to specific requirements [*], I need a singly-linked list implementation that uses integer
I'm asking this out of curiosity rather than due to a real need to
I have an Event class. Due to the way dates are handled, we need
I have a make table query that is used to run a report (due
Got two versions of a query. Neither does quite what I need.. Trying to
I have two sets of data which I need to join, but there is
I'm having an issue with a query that currently uses LEFT JOIN weblog_data AS
I'm having two tables, I need to select some data joined from both of
I'm using mysql/php/apache . I have the following situation: 2 tables where I need

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.