您的位置:首页 > 数据库 > MySQL

mysql 使用查询子句插入数据

2011-04-11 19:32 639 查看
清空
it_mac.goods it_mac.logistic

 

直接插入到运行中的库中

insert into ithelper.userInfo
(phone,departmentId,userName,remark)

 

select distinct phone,departmentId,name as
userName,'
综合
' as remark

from it_mac.xingzheng
as new

 

where not exists(

        
select
* from ithelper.userInfo

        
where
phone = new.phone

);

 

 

增加货物信息
goods

其中
goodsId categoryId setting mac currentPersonId indexOfBatch
需要根据
new
表中的填

 

goodsId

goodsCategory

flag
,日期为当前日期,批号为
Z
,货物号为累加

 

 

SET @NUM=0;

SET @GOODSID =date_format
(

now
(

)

,

'%Y%m%d'

);

Insert into it_mac.goods
(goodsId,providerId,departmentId,categoryId,setting,location,mac,state,remark,

                           
currentPersonId,batchId,indexOfBatch)

 

select

CONCAT(c.flag,@GOODSID, 'Z', right
(concat('0000', @NUM+1),3)) as
goodsId,

2 as providerId,

new.departmentId,

new.categoryId,

new.setting,

'IT
仓库
' as location,

new.mac,

1 as state,

new.remark,

u.userIndex as currentPersonId,

26 as batchId,

@NUM:=@NUM+1 as indexOfBatch

 

from it_mac.xingzheng

as new

left join 

ithelper.userInfo
as u on 
u.phone = new.phone

left join ithelper.goodsCategory

as c on 
c.categoryId =
new.categoryId

 

 

 

 

添加
logistic
记录

Insert into itHelper.logistic
(goodsId,userIndex,time,departmentId,state,remark)

Select g.goodsId,u.userIndex,now() as time,u.departmentId,g.state,g.remark
 

from it_mac.goods
as g

Left join itHelper.userInfo
as u on u.userIndex = g.currentPersonId
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql insert join date c