多表求和
1.有一个用户表users,两个文章表articles_1,articles_2。
如果得出articles_1中的tickets字段以及articles_2中的tickets字段的和。条件是与users中userid相同的记录。
select userid,
articlesCount = isnull((select sum(A1.tickets) from articles_1 A1 where A1.userid = U.userid),0) +
isnull((select sum(A2.tickets) from articles_2 A2 where A2.userid = U.userid),0)
from users U
可以查出users表中所有user的articles的数量,列articlesCount的值就是这个和!
2.select
(select count(*) from a)as info_1,
(select sum(Hits) from a) as info_2,
(select count(*) from b) as info_3,
(select count(*) from c) as info_4