>데이터 베이스 >MySQL 튜토리얼 >MySQL将表a中查询的数据插入到表b中_MySQL

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

WBOY
WBOY원래의
2016-06-01 13:01:581299검색

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

<code class="sql">如果表b存在

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

<code class="sql"><code class="sql"><code class="sql">如果表b不存在

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

<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">将b表中的某写字段值插入到a表中

<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">insert into a (userID,userName) select b.userID,b.userName from tr_ajax_chat_messages;</code></code></code></code></code></code></code></code></code></code></code>

<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">将a表和b表userID相等的值保存到a表

<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"><code class="sql">update a set a.userName=(select b.userName from b where a.userID = b.userID);</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.