Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Get SUM of COUNTs for several tables

OK, so here's what I need :

Let's say we've got a table, e.g. A and need to get the number of rows.

In that case we'd SELECT COUNT(*) FROM A.

Now, what if we have 3 different tables, let's say A, B and C.

How can I get the total number of rows in all three of them?

Upvotes: 0

Views: 50

Answers (3)

Anto Raja Prakash
Anto Raja Prakash

Reputation: 1388

select SUM(cnt) from (
select count(*) as cnt from A
union
select count(*) as cnt  from B
union
select count(*) as cnt  from C) as T

Upvotes: 0

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16076

Try something like this:

SELECT 
((select count(*) from demo1) +
(select count(*) from demo2) +
(select count(*) from demo3)) as Tbl3;

Sql fiddle:http://sqlfiddle.com/#!2/d439f8/5

Upvotes: 1

ps2goat
ps2goat

Reputation: 8475

Select (select count(*) from a) + 
       (select count(*) from b) + 
       (select count(*) from c);

is the easiest way if you only have 3 counts every time.

Upvotes: 3

Related Questions