Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum multiple values from multiple tables

Tags:

sql

postgresql

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?

like image 372
Diego Faria Avatar asked Jun 28 '26 12:06

Diego Faria


1 Answers

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
like image 95
ypercubeᵀᴹ Avatar answered Jul 01 '26 01:07

ypercubeᵀᴹ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!