开发者

How to count rows from multiple tables MySQL

开发者 https://www.devze.com 2023-03-15 18:28 出处:网络
How can开发者_StackOverflow社区 I count all the rows from 2 tables where... `approved` == \'0\' ?You need to do an aggregate on a union:

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";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号