从已创建的表中选择特定行创建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;
阅读更多:MySQL 教程
输出
+------+-----------+----------+------+
| 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)