MySQL如何计算多个表的行数

发布于 2024-11-17 01:13:42 字数 82 浏览 2 评论 0原文

如何计算 2 个表中的所有行,其中...

`approved` == '0'

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

`approved` == '0'

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的軌跡 2024-11-24 01:13:42

您需要对联合进行聚合:

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 )

还有其他方法,但这非常清楚地表明您的意图,特别是以这种方式命名列。

希望这有帮助

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

站稳脚跟 2024-11-24 01:13:42

您可以对每个表执行此操作:

SELECT count(*) FROM table WHEREroved = 0

您可以将其合并到一个查询中,但这会变得更加复杂。使用子查询或联合。

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.

嘿看小鸭子会跑 2024-11-24 01:13:42

这个答案将帮助您:

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

This answer will help you:

$query = "select count(*)+(select count(*) from table1) as totalrows from table2";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文