将sql中的值求和并将其显示在asp.net中 [英] sum values in sql and show it in asp.net

查看:75
本文介绍了将sql中的值求和并将其显示在asp.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询
从表中对记录进行计数

从表_1中选择COunt(*)

和另一个查询
用来计数其他表中的记录

从表_2中选择COunt(*)

如何获取总记录
我的意思是两个查询的总和

表_1的记录和表_2的记录
在单个字段中添加

仅使用sql查询

i have a query
which counts the records from a table

select COunt(*) from table_1

and another query
which counts the records from other table

select COunt(*) from table_2

how can I get total records
i mean that sum of both the queries

record of table_1 and record of table_2
added in single field

using only a sql query

推荐答案

DECLARE @sum1 int
DECLARE @sum2 int

SET @Sum1 = SELECT COUNT(*) FROM Table1
SET @Sum2 = SELECT COUNT(*) FROM Table2

SELECT 
      @Sum1 As SumTable1, 
      @Sum2 as SumTable2, 
      @Sum1 + @Sum2 As Total



或只是



or just

SELECT (SELECT COUNT(*) FROM Table_1) As count1,
       (SELECT COUNT(*) FROM Table_2) As count2,
       (SELECT COUNT(*) FROM Table_1)+(SELECT COUNT(*) FROM Table_2) as total



只有总数



only the total

SELECT (SELECT COUNT(*) FROM Table_1)+(SELECT COUNT(*) FROM Table_2) as total




请尝试以下查询:



Try below query:

SELECT SUM(cnt) FROM (
SELECT COUNT(*) cnt FROM table_1
UNION ALL
SELECT COUNT(*) cnt FROM  table_2) tbl



您好兄弟,使用此查询.........

Hello dude use this query.........

SELECT
(SELECT Count(*) FROM tab1  ) +
(SELECT Count(*) FROM tab2   )  as new_columnname



否则,您将像............]
一样使用此代码



otherwise u will use this code as like ............]


SELECT SUM(Coun)as Total FROM (
SELECT COUNT(*) Coun FROM tab1    
UNION ALL
SELECT COUNT(*)  Coun  FROM  tab2   ) tbl


这篇关于将sql中的值求和并将其显示在asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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