從已經建立的表格中選擇特定行來建立 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)
廣告