web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码。
 
以mysql数据库为例分情况一一说明:
两张表:insertTest和insertTest2,前者中有测试数据
create table insertTest(id int(4),name varchar(12));
insert into insertTest values(100,'tom');
insert into insertTest values(101,'tim');
insert into insertTest values(102,'sam');
1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:
  INSERT INTO 目标表 SELECT * FROM 来源表;
insert into insertTest1 select * from insertTest2;
2.如果只希望导入指定字段,可以用这种方法:
 INSERT INTO 目标表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 来源表;
 一定要注意: 字段的顺序必须一致
insert into insertTest1(id,name) select id,nickname from insertTest2;
3.如果您需要只导入目标表中不存在的记录,可以使用这种方法:
 INSERT INTO 目标表  
 (字段1, 字段2, ...)  
 SELECT 字段1, 字段2, ...  
 FROM 来源表  
 WHERE not exists (select * from 目标表  
 where 目标表.比较字段 = 来源表.比较字段); 
 1>.插入多条记录:
insert into insertTest2
(id,name)
select id,name
from insertTest
where not exists (select * from insertTest2
where insertTest2.id=insertTest.id);

2>.插入一条记录:

insert into insertTest    
(id, name)    
SELECT 100, 'susu'    
FROM test    
WHERE not exists (select * from insertTest    
where insertTest.id = 100);
使用 test 作表名,select 语句后面直接跟上要插入的字段的值。

MySQL 数据库中如何把A表的数据插入到B表?的更多相关文章

  1. mysql数据库中,通过mysqldump工具仅将某个库的所有表的定义进行转储

    需求描述: 在研究mysqldump工具的使用,想的是如何将某个库下的,或者某个表的表的定义(表结构创建语句)进行转储 操作过程: 1.通过--no-data参数,就可以将某个库的表定义进行转储 [m ...

  2. 从mysql数据库中查询最新的一条数据的方法

    第一种方法 SELECT * from a where id = (SELECT max(id) FROM a); 第二种方法: select * FROM 表名 ORDER BY id DESC L ...

  3. mysql 从一个表查询数据插入另一个表或当前表

    mysql insert into 表明(uid,lng,lat) SELECT uuid,lng,lat FROM 表明

  4. 数据结构中La表的数据合并到Lb表中

    实验描述:La表中的数据为(3,5,8,11)  Lb 表中的数据为(2,6,8,9,11,15,20) 将La表中的数据而不存在Lb表的数据插入到Lb表中,从而实现并集操作. 出现的问题:最后实现的 ...

  5. 在mysql数据库中制作千万级测试表

    在mysql数据库中制作千万级测试表 前言: 最近准备深入的学一下mysql,包括各种引擎的特性.性能优化.分表分库等.为了方便测试性能.分表等工作,就需要先建立一张比较大的数据表.我这里准备先建一张 ...

  6. mysql管理 ------查看 MySQL 数据库中每个表占用的空间大小

    如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE ...

  7. 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据

    在mysql数据库中创建oracle scott用户的四个表及插入初始化数据 /* 功能:创建 scott 数据库中的 dept 表 */ create table dept( deptno int ...

  8. MySql数据库中,判断表、表字段是否存在,不存在就新增

    本文是针对MySql数据库创建的SQL脚本,别搞错咯. 判断表是否存在,不存在就可新增 CREATE TABLE IF NOT EXISTS `mem_cardtype_resource` ( ... ...

  9. 在mysql数据库中创建Oracle数据库中的scott用户表

    在mysql数据库中创建Oracle数据库中的scott用户表 作者:Eric 微信:loveoracle11g create table DEPT ( DEPTNO int(2) not null, ...

随机推荐

  1. uml设计之多重性

    ---------------------------------------------------------------------------------------------------- ...

  2. iOS将image转90,180,270度的方法

    http://blog.sina.com.cn/s/blog_6602ffbc0101ckx3.html 这里要分享的是将image旋转,而不是将imageView旋转,原理就是使用quartz2D来 ...

  3. 2018年DDoS攻击全态势:战胜第一波攻击成“抗D” 关键

    2018年,阿里云安全团队监测到云上DDoS攻击发生近百万次,日均攻击2000余次.目前阿里云承载着中国40%网站,为全球上百万客户提供基础安全防御.可以说,阿里云上的攻防态势是整个中国攻防态势的缩影 ...

  4. @spoj - ADAMOLD@ Ada and Mold

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 N 的序列 A,将其划分成 K + 1 段,划分 ...

  5. Convert Sorted Array to Binary Search Tree转换成平衡二查搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 二分 ...

  6. 2018-8-10-三种方式设置特定设备UWP-XAML-view

    title author date CreateTime categories 三种方式设置特定设备UWP XAML view lindexi 2018-08-10 19:16:52 +0800 20 ...

  7. 纯CSS3打造圆形菜单

    原理是使用相对定位和绝对定位确定圆形菜单位置. 使用伪类选择器E:hover确定悬浮时候的效果,动画效果用CSS3的transition属性. 大概代码如下. html: <div id=&qu ...

  8. 手写call,bind,apply

    //实现call var that = this ; //小程序环境 function mySymbol(obj){ let unique = (Math.random() + new Date(). ...

  9. VS开发ArcEngine时的一个异常信息——“ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components.”

    问题描述:程序报错“ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS ...

  10. hibernate 出现Caused by: java.sql.SQLException: Column 'id' not found.异常

    用hibernate进行映射查询时,出现Caused by: java.sql.SQLException: Column 'id' not found 异常,检查数据库表及映射都有id且已经正确映射, ...