Home >Database >Mysql Tutorial >MySQL将表a中查询的数据插入到表b中

MySQL将表a中查询的数据插入到表b中

WBOY
WBOYOriginal
2016-06-07 14:54:021499browse

MySQL将表a中查询的数据插入到表b中 如果表b存在 1 code class=sqlcode class=sqlcode class=sqlinsert into b select * from a;/code/code/code 如果表b不存在 1 code class=sqlcode class=sqlcode class=sqlcode class=sqlcode class=sqlcode class=sqlc

MySQL将表a中查询的数据插入到表b中

如果表b存在

1

<code class="sql"><code class="sql">insert into b select * from a;

如果表b不存在

1

<code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql">create table b as select * from a;

扩展:

将b表中的某写字段值插入到a表中

1

<code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql">insert into a (userID,userName) select b.userID,b.userName from tr_ajax_chat_messages;

将a表和b表userID相等的值保存到a表

1<code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql"><code class="sql">update a set a.userName=(select b.userName from b where a.userID = b.userID);

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