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)
廣告
資料結構
計算機網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP