How to count rows from multiple tables MySQL
How can开发者_StackOverflow社区 I count all the rows from 2 tables where...
`approved` == '0'
?
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
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.
This answer will help you:
$query = "select count(*)+(select count(*) from table1) as totalrows from table2";
精彩评论