+ -
当前位置:首页 → 问答吧 → 已知A表,怎么生成B表,用sql语句实现?跪求!!!

已知A表,怎么生成B表,用sql语句实现?跪求!!!

时间:2011-12-06

来源:互联网

table A 三个字段为
id type value 
1 1 50
1 1 100
1 2 100
1 2 50
2 1 50
2 1 30
2 2 50
2 2 50

table B  
id type1_value type2_value
1 150 150
2 80 100

问题是:已知A表,怎么生成B表,用sql语句实现?

作者: luluandzhangzhang   发布时间: 2011-12-06

SQL code
create table B as select * from A;

作者: hllfl   发布时间: 2011-12-06

create table b as 
select id,
sum(if(type=1,value,0)) as type1_value,
sum(if(type=2,value,0)) as type2_value
from A
group by id

作者: ACMAIN_CHM   发布时间: 2011-12-06

SQL code
create table B(id,type1_value,type2_value) as select id,type,value from A;

作者: hllfl   发布时间: 2011-12-06

不好意思~

作者: hllfl   发布时间: 2011-12-06

如果是向b表插入呢?该怎么写?
引用 2 楼 acmain_chm 的回复:
create table b as
select id,
sum(if(type=1,value,0)) as type1_value,
sum(if(type=2,value,0)) as type2_value
from A
group by id

作者: luluandzhangzhang   发布时间: 2011-12-06

insert into b 
select id,
sum(if(type=1,value,0)) as type1_value,
sum(if(type=2,value,0)) as type2_value
from A
group by id

作者: ACMAIN_CHM   发布时间: 2011-12-06