什么是MySQL SELECT INTO等效项?
SELECTINTO等效项是CREATETABLEASSELECT语句。语法如下-
CREATE TABLE yourNewTableName AS SELECT *FROM yourTableName;
为了理解上述概念,让我们创建一个表。创建表的查询如下-
mysql> create table selectIntoEquivalentDemo -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(20), -> ClientAge int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Larry',34); mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Maxwell',44); mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('Bob',38); mysql> insert into selectIntoEquivalentDemo(ClientName,ClientAge) values('David',39);
使用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中的SELECTINTO等效查询-
mysql> create table Client_information AS select *from selectIntoEquivalentDemo; 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)