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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:17:21+00:00 2026-06-06T19:17:21+00:00

I am running two queries to my database for pagination reasons. As such, each

  • 0

I am running two queries to my database for pagination reasons. As such, each query is nearly identical. My COUNT(*) query is not returning the number of results that the non-count query is. I’m baffled as to why this is the case. The queries are below.

SELECT p.host_id, p.rating_support, p.rating_tech, MAX(p.rating_overall) AS rating_overall, p.publish_rating, h.name, prices.price, prices.term_duration
FROM plans p
INNER JOIN hosts AS h ON h.id = p.host_id
INNER JOIN (SELECT plan_id, price, term_duration FROM prices WHERE price > 0 AND price < 50 AND term_duration = 1) prices ON prices.plan_id = p.id
WHERE p.published = 1 AND h.published = 1
GROUP BY p.host_id
ORDER BY rating_overall desc LIMIT 0, 12

SELECT COUNT(*) AS count
FROM plans p
INNER JOIN hosts AS h ON h.id = p.host_id
INNER JOIN (SELECT plan_id, price, term_duration FROM prices WHERE price > 0 AND price < 50 AND term_duration = 1) prices ON prices.plan_id = p.id
WHERE p.published = 1 AND h.published = 1
GROUP BY p.host_id

I’m not an expert at MySQL. Besides the count not providing the correct number of results, the non-count query works perfectly.

Any light on this problem 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-06-06T19:17:23+00:00Added an answer on June 6, 2026 at 7:17 pm

    With the help of Dems’ comment (hunt down and upvote him somewhere :), I created this query. Notice that I removed the subquery, because it seemed unnecessary:

    SELECT
      COUNT( DISTINCT p.host_id )
    FROM       plans p
    INNER JOIN hosts h ON h.id = p.host_id
    INNER JOIN prices  ON prices.plan_id = p.id
                      AND prices.price > 0
                      AND prices.price < 50
                      AND prices.term_duration = 1
    WHERE p.published = 1
      AND h.published = 1
    

    My original answer:

    To get the number of total row, you have to wrap the GROUP BY query into an outer SELECT:

    SELECT COUNT(*)
    FROM (
      SELECT NULL -- we are just counting, so we need no actual data -> a bit faster
      FROM       plans p
      INNER JOIN hosts h ON h.id = p.host_id
      INNER JOIN prices  ON prices.plan_id = p.id
                        AND prices.price > 0
                        AND prices.price < 50
                        AND prices.term_duration = 1
      WHERE p.published = 1
        AND h.published = 1
      GROUP BY p.host_id
    ) AS all_rows_without_data
    

    Or you could use SQL_CALC_FOUND_ROWS + FOUND_ROWS()

    http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

    A SELECT statement may include a LIMIT clause to restrict the number
    of rows the server returns to the client. In some cases, it is
    desirable to know how many rows the statement would have returned
    without the LIMIT, but without running the statement again. To obtain
    this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT
    statement, and then invoke FOUND_ROWS() afterward:

    First, simply select the required rows, but add SQL_CALC_FOUND_ROWS:

    SELECT SQL_CALC_FOUND_ROWS
      p.host_id, p.rating_support, p.rating_tech,
      MAX(p.rating_overall) AS rating_overall,
      p.publish_rating, h.name, prices.price, prices.term_duration
    FROM       plans p
    INNER JOIN hosts AS h ON h.id = p.host_id
    INNER JOIN prices  ON prices.plan_id = p.id
                      AND prices.price > 0
                      AND prices.price < 50
                      AND prices.term_duration = 1
    WHERE p.published = 1 AND h.published = 1
    GROUP BY p.host_id
    ORDER BY rating_overall desc
    LIMIT 0, 12;
    

    Second, get the number of rows that would have been returned if there weren’t a LIMIT statement in the first query:

    SELECT FOUND_ROWS();
    

    Update: SQL_CALC_FOUND_ROWS + FOUND_ROWS() doesn’t seem very reliable, always returs zero for unknown reason (not just me: FOUND_ROWS() keeps returning 0 ):

    http://sqlfiddle.com/#!2/7304d/8

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

Sidebar

Related Questions

I'm running a MYSQL query in two steps. First, I get a list of
I'm wondering how I would go about running two separate queries within one mysqli_result.
I have two SQL queries that I'm running from a C# winform, and I
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,
I'm running two queries in my script room.php . Both are using MySQLi prepared
I am running 2 queries against an Informix database. The queries are hitting 4
Two seemingly identical queries (as far as a newbie like me can tell, but
Here I am running two instance of same program in two different terminals. The
I have a single Magento install running two different websites. One sells ebooks and
I have two different web applications running on two different instances of tomcat. I

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.