读书人

sql的2个面试题解决方案

发布时间: 2012-05-28 17:59:33 作者: rapoo

sql的2个面试题
问题1
a表
id name
1 xiaosan
2 xiaosi
b表
id name sex
1 xiaowu boy
2 xiaoliu boy
3 xiaoqi girl

语句select * from a,b查询出来几条记录
这个查出来是六条 为什么呢??

问题2
表A
id com
1 50
2 60
1 30
1 40
2 80
表B
id price
1 null
2 null

怎么把表A中的数据根据id分组后求出com的和 然后插入到表B对应的id的price里边

比如id为1的com和胃50+30+40=120
那么表B id为1的price就为120
id为2的同理...

[解决办法]

SQL code
---2.create table t1(    id int,    com int)create table t2(    id int,    price int)insert into t1 select 1,50 union allselect 2,60 union allselect 1,30 union allselect 1,40 union allselect 2,80 insert into t2 select 1,NULL union allselect 2,NULL----------------------------update t2 set price=s from t2,(select id,sum(com) s from t1 group by id)tmp where t2.id=tmp.id
[解决办法]
探讨
SQL code


--问题1:
相当于:cross jion?

读书人网 >SQL Server

热点推荐