从已经创建的表格中选择特定行来创建 MySQL 表格?
要从已创建的表中创建表,请使用 CREATE TABLE AS SELECT 语句。让我们首先创建一个表 -
mysql> create table DemoTable1318 -> ( -> Id int, -> FirstName varchar(10), -> LastName varchar(10), -> Age int -> ); Query OK, 0 rows affected (0.50 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1318 values(1,'Chris','Brown',21); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1318 values(2,'David','Miller',24); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1318 values(3,'Carol','Taylor',23); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1318;
输出
+------+-----------+----------+------+ | Id | FirstName | LastName | Age | +------+-----------+----------+------+ | 1 | Chris | Brown | 21 | | 2 | David | Miller | 24 | | 3 | Carol | Taylor | 23 | +------+-----------+----------+------+ 3 rows in set (0.00 sec)
以下是从已经创建的表中选择特定行来创建表的查询 -
mysql> create table DemoTable1319 -> as select *from DemoTable1318 -> where Age IN(21,23); Query OK, 2 rows affected (0.81 sec) Records: 2 Duplicates: 0 Warnings: 0
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1319;
输出
+------+-----------+----------+------+ | Id | FirstName | LastName | Age | +------+-----------+----------+------+ | 1 | Chris | Brown | 21 | | 3 | Carol | Taylor | 23 | +------+-----------+----------+------+ 2 rows in set (0.00 sec)
广告