MySQL SELECT INTO 的當量是什麼?


SELECT INTO 類似於 CREATE TABLE AS SELECT 語句。語法如下 −

CREATE TABLE yourNewTableName AS SELECT *FROM yourTableName;

為了理解以上概念,讓我們建立一個表。建立表的查詢如下 −

mysql> create table selectIntoEquivalentDemo
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20),
   -> ClientAge int
   -> );
Query OK, 0 rows affected (0.71 sec)

使用 insert 命令在表中插入一些記錄。查詢如下 −

mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Larry',34);
Query OK, 1 row affected (0.13 sec)
mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Maxwell',44);
Query OK, 1 row affected (0.06 sec)
mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Bob',38);
Query OK, 1 row affected (0.07 sec)
mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('David',39);
Query OK, 1 row affected (0.09 sec)

使用 select 語句顯示錶中的所有記錄。查詢如下 −

mysql> select *from selectIntoEquivalentDemo

輸出如下 −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
| 1        | Larry      | 34        |
| 2        | Maxwell    | 44        |
| 3        | Bob        | 38        |
| 4        | David      | 39        |
+----------+------------+-----------+
4 rows in set (0.00 sec)

以下是 MySQL 中 SELECT INTO 的查詢等價物 −

mysql> create table Client_information AS select *from selectIntoEquivalentDemo;
Query OK, 4 rows affected (0.57 sec)
Records: 4 Duplicates: 0 Warnings: 0

現在讓我們檢查新表中的表記錄。查詢如下 −

mysql> select *from Client_information;

輸出如下 −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
| 1        | Larry      | 34        |
| 2        | Maxwell    | 44        |
| 3        | Bob        | 38        |
| 4        | David      | 39        |
+----------+------------+-----------+
4 rows in set (0.00 sec)

更新於: 2019-07-30

300 瀏覽

開啟你的職業生涯 之路

透過完成這門課程來獲得認證

開始
廣告
© . All rights reserved.