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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:43:48+00:00 2026-06-10T15:43:48+00:00

I need to count the number of rows from different(!) tables and save the

  • 0

I need to count the number of rows from different(!) tables and save the results for some kind of statistic. The script is quite simple and working as expected, but I’m wondering if it’s better to use a single query with (in this case) 8 subqueries, or if I should use separate 8 queries or if there’s even a better, faster and more advanced solution…

I’m using MySQLi with prepared statements, so the single query could look like this:

$sql = 'SELECT
            (SELECT COUNT(cat1_id) FROM `cat1`),
            (SELECT COUNT(cat2_id) FROM `cat2`),
            (SELECT COUNT(cat2_id) FROM `cat2` WHERE `date` >= DATE(NOW())),
            (SELECT COUNT(cat3_id) FROM `cat3`),
            (SELECT COUNT(cat4_id) FROM `cat4`),
            (SELECT COUNT(cat5_id) FROM `cat5`),
            (SELECT COUNT(cat6_id) FROM `cat6`),
            (SELECT COUNT(cat7_id) FROM `cat7`)';

$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8);
$stmt->fetch();
$stmt->free_result();
$stmt->close();

while the seperate queries would look like this (x 8):

$sql = 'SELECT
            COUNT(cat1_id)
        FROM
            `cat1`';

$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($var1);
$stmt->fetch();
$stmt->free_result();
$stmt->close();

so, which would be faster or “better style” related to this kind of query (e.g. statistics, counter..)

  • 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-10T15:43:50+00:00Added an answer on June 10, 2026 at 3:43 pm

    Regarding to TerryE’s example and the advice to use multi_query(!), I checked the manual and changed the script to fit my needs.. finally I got a solution that looks like this:

    $sql  = 'SELECT COUNT(cat1_id) as `cat1` FROM `cat1`;';
    $sql .= 'SELECT COUNT(cat2_id) as `cat2` FROM `cat2`;';
    $sql .= 'SELECT COUNT(cat2_id) as `cat2_b` FROM `cat2` WHERE `date` >= DATE(NOW());';
    $sql .= 'SELECT COUNT(cat3_id) as `cat3` FROM `cat3`;';
    $sql .= 'SELECT COUNT(cat4_id) as `cat4` FROM `cat4`;';
    $sql .= 'SELECT COUNT(cat5_id) as `cat5` FROM `cat5`;';
    $sql .= 'SELECT COUNT(cat6_id) as `cat6` FROM `cat6`;';
    $sql .= 'SELECT COUNT(cat7_id) as `cat7` FROM `cat7`;';
    
    if ($db->multi_query($sql))
    { 
        do
        {
            if ($stmt = $db->store_result())
            {
                while ($row = $stmt->fetch_assoc())
                {
                    foreach ($row as $key => $value)
                    {
                        $count[$key] = $value;
                    }
                }
                $stmt->free_result();
            }
        } while ($db->more_results() && $db->next_result());
    }
    

    There are some differences to TerryE’s example, but the result is the same. I’m aware that there are 7 line at the beginning that are almost identical, but as soon as I need a WHERE clause or something else, I prefer this solution to a foreach loop where I’d need to add queries manually or use exceptions with if { ... } …

    As far as I can see, there should be no problem with my solution, or did I miss something?

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

Sidebar

Related Questions

I need to count the number of rows returned from database.By using following code
I've got some tests that need to count the number of warnings raised by
I need to count the total number of rows in a table before the
I need to count number of repeating and position where they repeat of all
I have an array in jQuery, and I need to count the number of
In a table members(id, contractid, a,b,c,d) I need to count: the number of members
I need to count the total number of instances in which a 10-digit number
I need a regex to count the number of columns in a pipe delimited
I need a fast way to count the number of bits in an integer
Is there a way to get the number of rows 'returned' from a stored

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.