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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:59:43+00:00 2026-05-23T08:59:43+00:00

How do i combine the counts from all the tables being used in a

  • 0

How do i combine the counts from all the tables being used in a UNION query. This is what i have:

$query = "SELECT COUNT(*) as num
from table_one LEFT JOIN table_constant on table_one.c_id 
= table_constant.c_id 
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_two LEFT JOIN table_constant on table_two.c_id 
= table_constant.c_id 
where table_two.added_by = '$uid'
UNION
SELECT COUNT(*) as num
from table_three LEFT JOIN table_constant ON table_three.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_four LEFT JOIN table_constant ON table_four.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
ORDER BY date_time_added DESC";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
  • 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-23T08:59:43+00:00Added an answer on May 23, 2026 at 8:59 am

    Did you try to put count outside and apply it on sub query containing all tables union result.

     SELECT COUNT(*) FROM (SELECT ...) as abc
    

    Or try this out

    Select mytable .userid, sum(mytable .subcount) as totalcount from
    (
    select userid, count(*) as subcount from table1 group by userid
    union all
    select userid, count(*) as subcount from table2 group by userid
    )
    as mytable 
    group by mytable .userid
    

    or try using FULL OUTER JOIN instead of union it will give you the same result..

     SELECT Count(UserID), UserId
     FROM MyTable1
     GROUP BY MyTable1.UserID
     UNION
     SELECT Count(UserID), UserId
     FROM MyTable2
     FULL OUTER JOIN MyTable2 ON (MyTable1.UserId=MyTable2.UserId)
     GROUP BY MyTable2.UserID
    

    Updated answer:

    IF Your query is working fine follow my first option that i gave means

     select count(*) from (your query) as pagecount..
    

    Then your query would be like this…..

      select count(*) from
      (
      SELECT COUNT(*) as num
       from table_one LEFT JOIN table_constant on table_one.c_id 
      = table_constant.c_id 
      where table_constant.user_id = '$uid'
     UNION
     SELECT COUNT(*) as num
    from table_two LEFT JOIN table_constant on table_two.c_id 
     = table_constant.c_id 
     where table_two.added_by = '$uid'
    UNION
    SELECT COUNT(*) as num
    from table_three LEFT JOIN table_constant ON table_three.c_id 
    = table_constant.c_id
    where table_constant.user_id = '$uid'
    UNION
    SELECT COUNT(*) as num
    from table_four LEFT JOIN table_constant ON table_four.c_id 
    = table_constant.c_id
    where table_constant.user_id = '$uid'
    ORDER BY date_time_added DESC") as pagecount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query, looks something like this: select name, count (*) from
Under the my query.I select this way all job count by fullname. SELECT COUNT(sy.FullName)
i have this piece of code that selects all users from the table and
How do you combine multiple select count(*) from different table into one return? I
I have 3 queries: table: pageview SELECT event_id, count(*) AS pageviews FROM pageview GROUP
I'm trying to combine a list of functions like so. I have this: Func<int,
I'm trying to combine data from two tables in MySQL with PHP. I want
I have a query that totals up all cases that were active on 01/01/2010.
I have 2 queries which return counts of different information in a table: SELECT
I have 4 tables namely, countries, states, cities, areas, apart from countries table the

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.