如何從一張 MySQL 表格插入資料到另一個表格並設定一個列值?
我們首先來建立一張表格。以下為查詢 −
mysql> create table insertOneToAnotherTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.60 sec)
以下為使用 insert 命令在表中插入一些記錄的查詢 −
mysql> insert into insertOneToAnotherTable values(100); Query OK, 1 row affected (0.08 sec) mysql> insert into insertOneToAnotherTable values(200); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable values(300); Query OK, 1 row affected (0.13 sec) mysql> insert into insertOneToAnotherTable values(400); Query OK, 1 row affected (0.15 sec) mysql> insert into insertOneToAnotherTable values(500); Query OK, 1 row affected (0.12 sec) mysql> insert into insertOneToAnotherTable values(600); Query OK, 1 row affected (0.16 sec)
以下為使用 select 語句顯示錶格中所有記錄的查詢 −
mysql> select * from insertOneToAnotherTable;
這將產生以下輸出 −
+-------+ | Value | +-------+ | 100 | | 200 | | 300 | | 400 | | 500 | | 600 | +-------+ 6 rows in set (0.00 sec)
以下是建立第二個表格的查詢 −
mysql> create table recieveDateFromTable -> ( -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.83 sec)
以下是從一張 MySQL 表格中插入到另一張表格中並設定一個列值查詢 −
mysql> insert into recieveDateFromTable(Value1,Value2) select Value,1000 from insertOneToAnotherTable; Query OK, 6 rows affected (0.14 sec) Records: 6 Duplicates: 0 Warnings: 0
讓我們顯示所有來自第二張表格的記錄。以下為查詢 −
mysql> select * from recieveDateFromTable;
這將產生以下輸出 −
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 100 | 1000 | | 200 | 1000 | | 300 | 1000 | | 400 | 1000 | | 500 | 1000 | | 600 | 1000 | +--------+--------+ 6 rows in set (0.00 sec)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP