943,829 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 6384
  • MySQL RSS
Apr 18th, 2009
0

Trying to get row count from multiple tables

Expand Post »
I have around 20 tables in my database. I want to count every row of every table using COUNT and get a total. I have tried doing a separate select statement for each table then using UNION but I cannot find a way to join the results to get just one total number. Another thing I want to mention is that a lot of the tables are empty, but will later have data in them. Could someone please give me an idea of what kind of query to do? Thanks.
Similar Threads
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Apr 18th, 2009
1

Re: Trying to get row count from multiple tables

does this work?

sql Syntax (Toggle Plain Text)
  1. SELECT COUNT(x.*) AS countx, COUNT(y.*) AS county, COUNT(z.*) AS countz, (countx+county+countz) AS total FROM mytable1 AS x, mytable2 AS y, mytable3 AS x

Edit: No. but the following works

sql Syntax (Toggle Plain Text)
  1. SELECT countx+county+countz AS total FROM
  2. (SELECT COUNT( * ) AS countx FROM table1) AS x,
  3. (SELECT COUNT( * ) AS county FROM table2) AS y,
  4. (SELECT COUNT( * ) AS countz FROM table3) AS z
Last edited by peter_budo; Apr 19th, 2009 at 5:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 21
Solved Threads: 5
Junior Poster
jakesee is offline Offline
130 posts
since Jul 2008
Apr 18th, 2009
0

Re: Trying to get row count from multiple tables

Worked great jake, thank you very much.
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Sep 1st, 2011
0

oca13th

SELECT (COUNT(x.*)+COUNT(y.*)+COUNT(z.*)) AS total FROM mytable1,mytable2, mytable3
Last edited by Oca13th; Sep 1st, 2011 at 5:03 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Oca13th is offline Offline
1 posts
since Sep 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Sql between ....querying dates
Next Thread in MySQL Forum Timeline: php and mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC