计数(*)多个表

发布于 2024-12-14 08:03:45 字数 856 浏览 2 评论 0原文

我正在浏览从多个表获取计数的解决方案,我遇到了以下答案:

SELECT COUNT(*),(SELECT COUNT(*) FROM table2) FROM table1

它效果很好,但我似乎无法让它适用于超过 2 个表。我当前的代码如下:

SELECT COUNT(*), 
(SELECT COUNT(*) FROM TABLE1),
(SELECT COUNT(*) FROM TABLE2),
(SELECT COUNT(*) FROM TABLE3),
(SELECT COUNT(*) FROM TABLE4),
(SELECT COUNT(*) FROM TABLE5),
(SELECT COUNT(*) FROM TABLE6),
(SELECT COUNT(*) FROM TABLE7),
(SELECT COUNT(*) FROM TABLE8),
(SELECT COUNT(*) FROM TABLE9),
(SELECT COUNT(*) FROM TABLE10),
(SELECT COUNT(*) FROM TABLE11),
(SELECT COUNT(*) FROM TABLE12),
(SELECT COUNT(*) FROM TABLE13),
(SELECT COUNT(*) FROM TABLE14),
(SELECT COUNT(*) FROM TABLE15),
(SELECT COUNT(*) FROM TABLE16),
(SELECT COUNT(*) FROM TABLE17),
(SELECT COUNT(*) FROM TABLE18)
FROM TABLE19

但是,它只计算TABLE1和TABLE19。我需要计算所有表(表 1-18)以及表 19(希望使用与第一个示例类似的结构)。

I was browsing for a solution to getting counts from multiple tables and I came across the following answer:

SELECT COUNT(*),(SELECT COUNT(*) FROM table2) FROM table1

It works great, however I can't seem to get it to work for more than just 2 tables. My current code is as follows:

SELECT COUNT(*), 
(SELECT COUNT(*) FROM TABLE1),
(SELECT COUNT(*) FROM TABLE2),
(SELECT COUNT(*) FROM TABLE3),
(SELECT COUNT(*) FROM TABLE4),
(SELECT COUNT(*) FROM TABLE5),
(SELECT COUNT(*) FROM TABLE6),
(SELECT COUNT(*) FROM TABLE7),
(SELECT COUNT(*) FROM TABLE8),
(SELECT COUNT(*) FROM TABLE9),
(SELECT COUNT(*) FROM TABLE10),
(SELECT COUNT(*) FROM TABLE11),
(SELECT COUNT(*) FROM TABLE12),
(SELECT COUNT(*) FROM TABLE13),
(SELECT COUNT(*) FROM TABLE14),
(SELECT COUNT(*) FROM TABLE15),
(SELECT COUNT(*) FROM TABLE16),
(SELECT COUNT(*) FROM TABLE17),
(SELECT COUNT(*) FROM TABLE18)
FROM TABLE19

However, it only counts TABLE1 and TABLE19. I need to count all tables (TABLE1-18) as well as TABLE19 (hopefully using a structure to similar to the first example).

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

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

发布评论

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

评论(4

遮云壑 2024-12-21 08:03:45

根据哪个数据库,它可能会略有变化...

对于Oracle,请执行以下操作:

Select (select count(*) from table1) as table1Count, 
       (select count(*) from table2) as table2Count
from dual

如果是SQL Server,则不要使用dual。

编辑:

既然您在评论中提到您正在使用MySQL:

获取MySQL数据库中所有表的记录数

Depending on which DB this is it could slightly change...

For Oracle do something like:

Select (select count(*) from table1) as table1Count, 
       (select count(*) from table2) as table2Count
from dual

If it's SQL Server then just leave off the from dual.

EDIT:

Since you mentioned you are using MySQL in the comments:

Get record counts for all tables in MySQL database

長街聽風 2024-12-21 08:03:45

使用别名使列具有唯一的名称:

SELECT 
    (SELECT COUNT(*) FROM TABLE1) AS count_table1,
    (SELECT COUNT(*) FROM TABLE2) AS count_table2,
    (SELECT COUNT(*) FROM TABLE3) AS count_table3,
    etc..
    (SELECT COUNT(*) FROM TABLE19) AS count_table19

Use aliases so that the columns have unique names:

SELECT 
    (SELECT COUNT(*) FROM TABLE1) AS count_table1,
    (SELECT COUNT(*) FROM TABLE2) AS count_table2,
    (SELECT COUNT(*) FROM TABLE3) AS count_table3,
    etc..
    (SELECT COUNT(*) FROM TABLE19) AS count_table19
终难愈 2024-12-21 08:03:45

如果您愿意接受行而不是列中的结果,您可以随时使用 UNION:

SELECT "table1", COUNT(*) FROM table1 UNION                         
SELECT "table2", COUNT(*) FROM table2 UNION                         
SELECT "table3", COUNT(*) FROM table3

等等。

If you are happy to accept the results in rows, not columns, you can always use a UNION:

SELECT "table1", COUNT(*) FROM table1 UNION                         
SELECT "table2", COUNT(*) FROM table2 UNION                         
SELECT "table3", COUNT(*) FROM table3

etc.

郁金香雨 2024-12-21 08:03:45

在 MySQL 上,这是有效的:

select sum(total) from(
select count(*) as total from Table1 
union
select count(*) as total from Table2) as a;

On MySQL this works:

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