首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql的2个面试题解决方案

2012-05-28 
sql的2个面试题问题1a表id name1xiaosan2xiaosib表id name sex1xiaowuboy2xiaoliuboy3xiaoqigirl语句selec

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?

热点排行