当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql 从一个表中查数据并插入另一个表实现方法

mysql 从一个表中查数据并插入另一个表实现方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

mysql 从一个表中查数据并插入另一个表实现方法

不管是在网站开发还是在应用程序开发中,我们经常会碰到需要将mysql或ms sqlserver某个表的数据批量导入到另一个表的情况,甚至有时还需要指定导入字段。

本文就将以mysql数据库为例,介绍如何通过sql命令行将某个表的所有数据或指定字段的数据,导入到目标表 中。此方法对于sqlserver数据库,也就是t-sql来说,同样适用 。

类别一、 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法:

insert into  目标表  select  * from  来源表 ;

例如,要将 articles 表插入到 newarticles 表中,则可以通过如下sql语句实现:

insert into  newarticles  select  * from  articles ;

类别二、 如果只希望导入指定字段,可以用这种方法:

insert into  目标表 (字段1, 字段2, ...)  select   字段1, 字段2, ...   from  来源表 ;

请注意以上两表的字段必须一致,否则会出现数据转换错误。

insert into tpersonnelchange(  
   userid, 
   depid, 
   subdepid, 
   postiontype, 
   authorityid, 
   changedates, 
   insertdate, 
   updatedate, 
   sakuseisyaid 
 )select 
   userid, 
   depid, 
   subdepid, 
   postiontype, 
   authorityid, 
   date_format(employdate, '%y%m%d'), 
   now(), 
   now(), 
   1 
from 
   tusermst where 
   `status` = 0 
and quitflg = 0 
and userid > 2 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网