使用其他 table 的记录数创建 table

Creating a table with number of records of other tables

我想创建一个包含 2 列 TABLE_NAMECOUNT 的 table RECORDS_COUNT,它显示 5 table 的行数:

TABLE1TABLE2、..TABLE5POP-TOTAL.

例如:

TABLE-NAME |  COUNT
 ----------------------

TABLE1     |  200 
----------------------
TABLE2     |  100  
----------------------
TABLE3     |  200 
----------------------
TABLE4     |  350 
--------------------- 
TABLE5     |  150  
----------------------
POP-TOTAL      |  1000 

这些数字每天都会变化,我需要快速访问结果而无需打开 5 tables。

为了感兴趣,我在下面留下了我的旧答案,但我认为这更准确地解决了你的问题:

SELECT 
[TableName] = so.name, 
[RowCount] = MAX(si.rows)
into #temp
FROM 
sysobjects so, 
sysindexes si 

WHERE 
so.xtype = 'U' 
AND 
si.id = OBJECT_ID(so.name)
and so.name in ('TABLE1', 'TABLE2', 'TABLE3', 'TABLE4', 'TABLE5') 
GROUP BY 
so.name 

select * 
from #temp
union all
select 'POP-TOTAL', (select SUM([RowCount]) from #temp)

这将在临时 table 中产生您想要的结果。然后总结出来展示出来

旧答案:

如果格式很重要,这可能对您不起作用,但这里有一个答案: Select COUNT in two table in one query with MYSQL

它可能会让你走上正轨。

select (select count(*) from table1) as t1_amount,
       (select count(*) from table2) as t2_amount,
       (select count(*) from table1) + (select count(*) from table2) as Total

使用这种方法,每个 table 一列,加上一列作为总数。

它不是最有效的,但如果您只是想每天检查一下,它应该可以胜任。

输出:

t1_amount | t2_amount | Total
-----------------------------
1121      | 544       | 1665