postgresql insert语句中用select

错误示例:

insert into userinfo (username,studentno,gender,brchid,cmpid,bldid,cardtype,rid)
select '晓红' as username,'201111321' as studentno, '女' as gender,1 as brchid,1 as cmpid,'正式卡' as cardtype,1 as bldid,
ri.rid as rid from roominfo ri 
join buildinginfo bi on bi.bldid=ri.bldid
join campusinfo ci on ci.cmpid=bi.cmpid
where rno='102'

userinfo中大部分信息是可以直接获得,但是字段rno得从buildinginfo,campusinfo,roominfo联合得到,于是很哈皮的在用select表联合的字段给了insert

当时想select出来的字段都as各自的名字了,应该好可以和insert要求的字段自动对应,如:studentno自动找到studentno,没有顺序的作用。

可是事实并不是这样,这句话输入以后一直报‘正式卡‘类型错误(不同顺序报错不一样)

顺序调整如下,就可以了

正确示例:

insert into userinfo (username,studentno,gender,brchid,cmpid,bldid,cardtype,rid)

select '晓红' as username,'201111321' as studentno, '女' as gender,1 as brchid,1 as cmpid,1 as bldid,'正式卡' as cardtype,
ri.rid as rid from roominfo ri 
join buildinginfo bi on bi.bldid=ri.bldid
join campusinfo ci on ci.cmpid=bi.cmpid

where rno='102'

看来是习惯了mybatis将查询结果的字段自动映射给pojo对象,才犯的错误。

你可能感兴趣的:(postgresql insert语句中用select)