Diego Faria
Diego Faria

Reputation: 9185

Sum multiple values from multiple tables

I'd like to do an SQL query to get a sum number, but i don't know how to construct this query.

select count(*) from table1 where commom_fk in (1234);
select count(*) from table2 where commom_fk in (1234);
select count(*) from table3 where commom_fk in (1234);
select count(*) from table4 where commom_fk in (1234);
select count(*) from table5 where commom_fk in (1234);

I wanna to sum these results in just one query, is that a way to do this?

Thank you all. -----*

This was answered. But if i wanna to do this with more than one common_fk?

Upvotes: 3

Views: 2407

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115620

SELECT     
      ( SELECT ...)
    + ( SELECT ...)
    + ( SELECT ...)
    + ( SELECT ...)
    + ( SELECT ...)    
  AS sumAll

or to have all 5 results:

SELECT     
      ( SELECT ...) AS sum1
    , ( SELECT ...) AS sum2
    , ( SELECT ...) AS sum3
    , ( SELECT ...) AS sum4
    , ( SELECT ...) AS sum5

Upvotes: 5

Related Questions