MySQL: A sum from 3 different tables - mysql

How can I sum the columns of 3 tables?
I have table1, table2, and table3 with the column 'revenue'.
I can do SELECT SUM(REVENUE) FROM TABLE1 but what do i do for all of them?
I tried: SELECT SUM(
table1.Revenue+
table2.Revenue +
table3.Revenue
)
FROM
table1,
table2,
table3' but it doesn't work...
Any ideas? Thanks!

select sum(rev) as trev
from
(
SELECT SUM( Revenue) as rev FROM table1
union all
SELECT SUM( Revenue) as rev FROM table2
union all
SELECT SUM( Revenue) as rev FROM table3
) as tmp

You need to add all of the individual sums together:
SELECT (SUM(table1.Revenue) + SUM(table2.Revenue) + SUM(table3.Revenue))
AS total_rev FROM table1, table2, table3

select sum( revenue )
from (
select revenue from table1
union
select revenue from table2
union
select revenue from table3
)

SELECT SUM(REVENUE) FROM (
SELECT REVENUE FROM TABLE1
UNION ALL
SELECT REVENUE FROM TABLE2
UNION ALL
SELECT REVENUE FROM TABLE3
) revenues

Related

How to take SUM of all COUNTs?

I am new to mysql.
I have a query to get count from different tables and it works okay. But, now I want to club all 3 query COUNT into 1 like SUM (COUNT1, COUNT2, COUNT3)
select COUNT(*) as count1, 0 as count2, 0 as count3 from table1
UNION ALL
select 0 as count1, COUNT(*) as count2, 0 as count3 from table2
UNION ALL
select 0 as count1, 0 as count2, COUNT(*) as count3 from table3
E.g. count1 = 10, count2 = 20 and count3 = 15
Then, I want to get sum = 45
You can put the UNION in a subquery and then SUM() the counts. But there's no need to put them in separate columns in each unioned query.
SELECT SUM(count) AS total
FROM (
SELECT COUNT(*) AS count FROM table1
UNION ALL
SELECT COUNT(*) AS count FROM table2
UNION ALL
SELECT COUNT(*) AS count FROM table3
) AS subquery
to get sum of all counts from you tables you can write a query as
select
(select COUNT(*) from table1) +
(select COUNT(*) from table2) +
(select COUNT(*) from table3) as total
Following your approach, you can wrap unioined query in as sub select and then apply sum on the results
SELECT SUM(count1) + SUM(count2) + SUM(count3) total
FROM (
SELECT COUNT(*) AS count1, 0 AS count2, 0 AS count3 FROM table1
UNION ALL
SELECT 0 AS count1, COUNT(*) AS count2, 0 AS count3 FROM table2
UNION ALL
SELECT 0 AS count1, 0 AS count2, COUNT(*) AS count3 FROM table3
) t

Efficient way to find maximum value on 80+ tables

I have a 80 tables and I want to filter who has the highest price among 80 tables. I plan on using this query:
SELECT id
FROM (SELECT id, price FROM T1 WHERE price = (SELECT MAX(price) FROM T1)
UNION
SELECT id, price FROM T2 WHERE price = (SELECT MAX(price) FROM T2)
UNION
SELECT id, price FROM T3 WHERE price = (SELECT MAX(price) FROM T3)
) AS M
ORDER BY price DESC
LIMIT 1
But I find it inefficient. Is there other way?
I think this would be a lot faster.
SELECT id, price FROM t1
UNION SELECT id, price FROM t2
UNION SELECT id, price FROM t3
ORDER BY price DESC
LIMIT 1

MYSQL total count more than 1 table total result

I have 3 tables. I want to select count in 1 result, like:
table1=1000 records + table2=400 records + table3=200 records = 1600
1600 is the one result I want back from the server.
MySQL inner join perhaps? Any suggestions?
Try this :
select sum(c) from (
select count(*) as c from table1
union
select count(*) as c from table2
union
select count(*) as c from table3
) tmp
That'll give you the total.
select
(
select count(columnname) from table1
) + (
select count(columnname) from table2
)+ (
select count(columnname) from table3
)
try this...,
SELECT (SELECT COUNT(*) FROM tbl1
)+
(SELECT COUNT(*) FROM tbl2
)+
( SELECT COUNT(*) FROM tbl3
) as 'AllCount'
select
((select count(*) from table1) + (select count(*) from table2) + (select count(*) from table3))
as totalCount;
Try this:
SELECT ((SELECT COUNT(*) FROM tbl1 ) + (SELECT COUNT(*) FROM tbl2 ) + (SELECT COUNT(*) FROM tbl3 )) AS TotalRecords
Thank you all for your responses I have 3 tables and i want to select a count in 1 single result
still i get results back like this
count1 count2 count3
1235 134 234
and this is not what i want a total one result

select into using union

I need to using both "union" and "into" in a query. These two versions works ok
SELECT x.* INTO
NewTABLE FROM
(SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2) x
Or
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, val2 FROM TABLE1 UNION SELECT val1, val2 FROM TABLE2) x
But what i need is something like this
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE1
UNION SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE2) x
It looks like the sum and/or Iif is the problem. How should I solve this problem!?
You forgot the GROUP BY in your union selects.
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE1 GROUP BY Val1
UNION
SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE2 GROUP BY Val1) x
You must either aggregate or GROUP BY all select fields in an aggregate query.

Mysql Distinct while selecting other column

SELECT DISTINCT ID FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4 ORDER BY total_hits DESC) AS sumtbl LIMIT 50;
This query works fine and selects unique ID's ordered by total_hits DESC, the question is how can I return total_hits column too having Id's unique?
SELECT ID, SUM(total_hits)
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
) AS sumtbl
GROUP BY ID
ORDER BY SUM(total_hits) DESC
LIMIT 50;
If you want the total_hits FROM all the ids across all the tables, you'll need to do a sum / group by. Not sure if this is what you're asking since the question is vague...
SELECT DISTINCT id, sum(total_hits) total_hits
FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4) AS sumtbl
GROUP BY id
ORDER BY total_hits DESC
LIMIT 50
Also, don't use select * as it's bad practice, esp. if you go to add a column to one of those tables and not the others, your whole query will break.
UPDATE
In your case you can do
SELECT ID, MAX(total_hits) as max_hits
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
)sumtbl
GROUP BY ID
ORDER BY max_hits DESC
LIMIT 50
Note : you don't need ORDER BY in derived query, it goes to upper
Also, it would be better, if you
SELECT id, total_hits
FROM tbl1
UNION ALL
SELECT id, total_hits FROM tbl2 etc. rather then selecting all fields from tables in your derived query.