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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:21:07+00:00 2026-05-12T09:21:07+00:00

I need to get the row count for individual SELECT statements in an UNION

  • 0

I need to get the row count for individual SELECT statements in an UNION ALL SELECT query that uses LIMIT. The MySQL docs are pretty clear about getting the global row count for the query (place individual SELECTs in parenthesis and place an SQL_CALC_FOUND_ROWS only in the first statement, then get FOUND_ROWS() the usual way). However, I also need the result row count for the individual SELECT statements. The query, simplified:

(SELECT SQL_CALC_FOUND_ROWS `field1`,`field2` FROM `db`.`table1` WHERE `id`>1000)
UNION ALL
(SELECT `field1`,`field2` FROM `db`.`table2` WHERE `id`>1000)
UNION ALL
...
(SELECT `field1`,`field2` FROM `db`.`tableN` WHERE `id`>1000)
LIMIT 0,10

If SQL_CALC_FOUND_ROWS is placed in every SELECT statement an “Incorrect usage/placement of ‘SQL_CALC_FOUND_ROWS'” error is issued.
Google this a lot, read related messages here, to no avail. Might be something really simple, I just can’t get my mind around it.

  • 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-12T09:21:07+00:00Added an answer on May 12, 2026 at 9:21 am

    You’re getting that error because SQL_CALC_FOUND_ROWS can be used on a query to return a single number into FOUND_ROWS().

    You can get back counts doing this:

    SELECT 'table1' AS tablename, COUNT(1) AS rowcount FROM table1 WHERE id > 1
    UNION ALL
    SELECT 'table2' AS tablename, COUNT(1) AS rowcount FROM table2 WHERE id > 1
    ...
    SELECT 'tableN' AS tablename, COUNT(1) AS rowcount FROM tableN WHERE id > 1
    

    if that helps.

    If you want to return the rows from the tables with a count then modify it:

    SELECT field1, field2, (SELECT COUNT(1) FROM table1 WHERE id > 1000) FROM table1 WHERE id > 1000
    UNION ALL
    SELECT field1, field2, (SELECT COUNT(1) FROM table2 WHERE id > 1000) FROM table2 WHERE id > 1000
    ...
    SELECT field1, field2, (SELECT COUNT(1) FROM tableN WHERE id > 1000) FROM tableN WHERE id > 1000
    

    From your question, it’s not precisely clearly what you’re trying to achieve.

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

Sidebar

Related Questions

I need to get the row count of a query, and also get the
I'm working on a project where I need to get the total row count
How Can i iterate thru the all my DB's and get a row count
i need to get the value of the selected row of radiobutton in the
I need get all items these have no categories int? categoryId = null; var
I need to get all the objects whose id matches a specific pattern .
I'm trying to make a customized view that gets me the row count for
I need to grab the result of a query, with php from a mysql
I'm using this php to fetch data from my mysql database: $wpdb->get_var(select count(*) from
Hi i'm trying to get a row count from a table using Watin ie

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.