如何计算来自多个表的记录数 [英] How to count number of records from multiple tables

查看:74
本文介绍了如何计算来自多个表的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,该查询从多个表中获取数据,现在我必须计算记录总数,我可以通过将表填充到DataTable对象(例如dt.Rows.Count)中来获取记录数,但是表包含一百万记录,因此需要时间来填充数据.
预先感谢.

i have a query that get the data from multiple tables now i have to count the total number of records,i can get number of records by filling the table into DataTable object e.g dt.Rows.Count,but table contains 1 million records so it takes time to fill data.
Thanks in advance.

推荐答案



在您的选择语句中使用@@ ROWCOUNT.喜欢,

从... join ......中选择@@ ROWCOUNT,col1,col2......
Hi,

Use @@ROWCOUNT in your select statement. like,

select @@ROWCOUNT,col1,col2..... from ... join.....


使用新查询对记录进行计数

选择Count(*)From ...您选择的语句

这样,您将不必等待时间来填充数据表.
当然,这会导致数据库的双重选择.
USe new query for count the record

Select Count(*) From ...your select statment

In this way you will not have to wait the time to fill the datatable.
Of course this leads to a double select the database.


我通过使用CTE(通用类型表达式)
解决了这个问题. 首先,我创建CTE名称cte
像例如:
字符串cte =; with mycte as(我从多个表中获取数据的语句)"
从mycte中选择count(*);

注意:CTE以分号开头
I solved this problem by using CTE(Common Type Expressions)
first i create CTE name cte
like e.g:
string cte=";with mycte as(my statement that take data from multiple table)"
select count(*) from mycte;

note:CTE start with semicolon


这篇关于如何计算来自多个表的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆