MySQL將表a中查詢的數據插入到表b中
如果表b存在
1
<code class="sql"><code class="sql"><code class="sql">insert into b select * from a;</code></code></code>
如果表b不存在
1
<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>
擴展:
將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"><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>
將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"><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>