日期:2014-05-18  浏览次数:20450 次

一个简单的查询,求高手指点!
SQL code


--DROP TABLE A
CREATE TABLE A
(
id int primary key identity(1,1),
score decimal(18,2)
)

--DROP TABLE B
CREATE TABLE B
(
id int primary key identity(1,1),
pid int ,  --A表的ID
math decimal(18,2),
english decimal(18,2)
)

SELECT * FROM A
INSERT INTO A 
VALUES(50)

SELECT * FROM B
INSERT INTO B 
VALUES(1,5,5)
INSERT INTO B 
VALUES(1,5,5)
INSERT INTO B 
VALUES(1,15,15)

--想获得子表中math总分
想获得SQL语句取得以下结果


id          score    math
----------- ---------------------------------------
1           50.00    ?




------解决方案--------------------
SQL code

select #a.id,#a.score,SUM(#b.math) as math 
    from #A,#B
    where #A.id = #B.pid
    group by #A.id,#A.score

------解决方案--------------------
SQL code
select A.ID,A.Score,SUM(math)as math  from A inner Join B on A.id=B.pid group by A.id,A.score

------解决方案--------------------
SQL code
select 
a.ID,a.score,
sum(math) as math
from A
inner join B on a.id=b.pid
group by a.ID,a.score

------解决方案--------------------
楼主烧糊涂了,开始胡说八道了
------解决方案--------------------
探讨

果真都是高手,多谢。都使用了Group By!
能不能挑战一下,不使用Group By.