2

I am writing a query in Mysql database, in which Query 1 returns count() say result is 10 and Query 2 returns Count() say result is 30

But I want to get the result as 40, which is sum of both

what are my options to get a single query giving me the result.

2 Answers 2

4

You should use UNION ALL to union also the same valued counts like 30+30.

select SUM(n) as total
from (
  (select count(*) as n from table1)
  UNION ALL
  (select count(*) as n from table2)
) t;
Sign up to request clarification or add additional context in comments.

Comments

1
select sum(num) as total
from (
  (select count(*) as num from table1)
  UNION ALL
  (select count(*) as num from table2)
) a;

3 Comments

If both subqueries return 30, it won't return 60, instead will return 30.
indeed. thanks for that. I'll make the correction... though you've already posted the answer yourself I see.
@Pentium, As mentioned above comments, what is the fix for If both subqueries return 30, it won't return 60, instead will return 30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.