Home >Database >Mysql Tutorial >MySQL下将一个表的数据插入到另外一个表的实现语句_MySQL

MySQL下将一个表的数据插入到另外一个表的实现语句_MySQL

WBOY
WBOYOriginal
2016-06-01 13:19:43983browse

bitsCN.com 如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:
Code:

INSERT INTO 目标表 SELECT * FROM 来源表;

比如要将 articles 表插入到 newArticles 表中,则是:

INSERT INTO newArticles SELECT * FROM articles;

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

INSERT INTO 目标表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 来源表;

注意字段的顺序必须一致。
如果您需要只导入目标表中不存在的记录,可以参考另外一篇文章
MySQL 当记录不存在时插入(insert if not exists)bitsCN.com

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn