1

How can I count all the rows from 2 tables where...

`approved` == '0'

?

3 Answers 3

4

You need to do an aggregate on a union:

select sum(subcount) as totalcount 
    from ( select count(*) as subcount from table1 where approved = 0
           union
           select count(*) as subcount from table2 where approved = 0 )

There are other ways but this shows your intent very clearly, especially naming the columns this way.

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

Will return incorrect value if the two sub-counts are identical. UNION ALL instead of UNION should fix the problem.
Throws the following error: Error: Every derived table must have its own alias
-1

You would do this for each table:

SELECT count(*) FROM table WHERE approved = 0

You could combine it into one query but that gets more complicated. Either use a subquery or union.

Comments

-1

This answer will help you:

$query = "select count(*)+(select count(*) from table1) as totalrows from table2";

Comments

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.