日期:2014-05-17  浏览次数:20522 次

一个简单的菜鸟问题,期待高手答复
表t1
c1 c2 c3
1 a b
2 c d
3 e f


用什么语句可以实现
c1 c2 c3 c4
1 a b 6 
2 c d 6
3 e f 6

就是说C4字段为C1字段的合计,然后跟原来表并一起显示出来

------解决方案--------------------
SQL code
CREATE TABLE #tb (c1 INT,  c2 VARCHAR(10),c3 VARCHAR(10) )
 INSERT INTO #TB select 1, 'a', 'b'
       union all select 2, 'c', 'd'
       union all select 3, 'e', 'f'
 SELECT a.* ,b.sc
 FROM #tb a,(SELECT SUM(c1) sc FROM #tb)b
 /*
 c1          c2         c3         sc
 ----------- ---------- ---------- -----------
 1           a          b          6
 2           c          d          6
 3           e          f          6
 
 (3 行受影响)
 
 */

------解决方案--------------------
SQL code
select a.*,b.c4
from tb a,
(select sum(c1) as c4 from tb) b