计数(*)多个表
我正在浏览从多个表获取计数的解决方案,我遇到了以下答案:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据哪个数据库,它可能会略有变化...
对于Oracle,请执行以下操作:
如果是SQL Server,则不要使用dual。
编辑:
既然您在评论中提到您正在使用MySQL:
获取MySQL数据库中所有表的记录数
Depending on which DB this is it could slightly change...
For Oracle do something like:
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
使用别名使列具有唯一的名称:
Use aliases so that the columns have unique names:
如果您愿意接受行而不是列中的结果,您可以随时使用 UNION:
等等。
If you are happy to accept the results in rows, not columns, you can always use a UNION:
etc.
在 MySQL 上,这是有效的:
On MySQL this works: