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

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 all
select 2,60 union all
select 1,30 union all
select 1,40 union all
select 2,80 

insert into t2 select 1,NULL union all
select 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?