select t1.* , (select count(1) from t_a t2 where t2.id = t1.id) + (select count(1) from t_b t3 where t3.id = t1.id) from t_c t1
那应该是视图吧。 create view vw_T_C as select c.*,(isnull(a.value1,0) + isnull(b.value2,0)) as [value] from T_C c left join T_A a on a.id=c.id left join T_B b on b.id=c.id go select * from vw_T_C
select *,isnull((select count(1) from T_A),0) + isnull((select count(1) from T_B))as value from T_C
select c.sid, *, a.cnt+b.cnt as value from t_c c, (select count(JinE1) as cnt from T_A) as a, (select count(JinE2) as cnt from T_B) as b
select c.*,isnull((select count(1) from T_A a where a.sid=c.sid),0) + isnull((select count(1) from T_B b where b.sid=c.sid),0)as value from T_C c
select t1.* , (select count(1) from t_a t2 where t2.sid = t1.sid) + (select count(1) from t_b t3 where t3.sid = t1.sid) from t_c t1如果可能存在不匹配的话,如楼上加isnull() select t1.* , isnull((select count(1) from t_a t2 where t2.sid = t1.sid),0) + isnull((select count(1) from t_b t3 where t3.sid = t1.sid),0) from t_c t1
from t_c t1
create view vw_T_C
as
select c.*,(isnull(a.value1,0) + isnull(b.value2,0)) as [value]
from T_C c
left join T_A a on a.id=c.id
left join T_B b on b.id=c.id
go
select * from vw_T_C
T_A sid, *, JinE1
T_B sid, *, JinE2
T_C sid, *
三个表并必然的关系。SQL语句出现的形式
select sid, *, count(JinE1) + count(JinE2)
from T_C
from T_C
select
c.sid,
*,
a.cnt+b.cnt as value
from t_c c,
(select count(JinE1) as cnt from T_A) as a,
(select count(JinE2) as cnt from T_B) as b
from T_C c
select t1.* , (select count(1) from t_a t2 where t2.sid = t1.sid) + (select count(1) from t_b t3 where t3.sid = t1.sid)
from t_c t1如果可能存在不匹配的话,如楼上加isnull()
select t1.* , isnull((select count(1) from t_a t2 where t2.sid = t1.sid),0) + isnull((select count(1) from t_b t3 where t3.sid = t1.sid),0)
from t_c t1