数据库查询以在单个查询中获取不同表的计数总和

发布于 2024-10-07 12:55:13 字数 139 浏览 2 评论 0原文

我正在Mysql数据库中编写一个查询,其中 查询 1 返回 count() 表示结果为 10 和 查询 2 返回 Count() 说结果是 30

但我想得到结果 40,这是

我获得单个查询给出结果的选项的总和。

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.

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

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

发布评论

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

评论(2

氛圍 2024-10-14 12:55:13

您应该使用 UNION ALL 来合并相同值的计数,例如 30+30。

select SUM(n) as total
from (
  (select count(*) as n from table1)
  UNION ALL
  (select count(*) as n from table2)
) t;

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;
神妖 2024-10-14 12:55:13
select sum(num) as total
from (
  (select count(*) as num from table1)
  UNION ALL
  (select count(*) as num from table2)
) a;
select sum(num) as total
from (
  (select count(*) as num from table1)
  UNION ALL
  (select count(*) as num from table2)
) a;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文