【发布时间】:2015-12-18 05:09:54
【问题描述】:
我在 SQL Server 中有 2 个表,Table1 和 Table2。
Table1 包含户主的年龄信息,Table2 包含家庭成员的年龄信息。
我想获得总年龄。表示如果用户在前端输入的人数少于 23,那么我将向用户显示两个表中年龄小于 23 的总人数。
我的做法是这样的:
alter procedure example
(@age int, @result2 int output)
as
begin
declare @result int;
declare @result1 int;
set @result = (select count(age) from Table1 where age > @age)
set @result1 = (select count(age) from Table2 where age > @age)
set @result2 = @result + @result1;
end
hvcid 是我的Table1 中的主键列,hvcid 是我的Table2 中的外键。
上面显示的我的查询返回结果很好。为什么不使用join或者single query来组合两张表,得到两张表的总年龄?
我不知道如何使用单个查询编写?你能解决我的问题吗?
【问题讨论】:
标签: c# sql-server