使用 ENUM 在包含多個選項的 MySQL 中選擇帶有 ACTIVE 狀態的記錄


讓我們首先建立一個表。在此,我們使用 ENUM 設定了狀態 −

mysql> create table DemoTable2037
   -> (
   -> StudentId int,
   -> status enum('Active','Inactive')
   -> );
Query OK, 0 rows affected (0.51 sec)

使用 insert 命令向表中插入一些記錄 −

mysql> insert into DemoTable2037 values(99,'Active');
Query OK, 1 row affected (0.08 sec)

mysql> insert into DemoTable2037 values(99,'Inactive');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable2037 values(100,'Inactive');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable2037 values(99,'Active');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable2037 values(100,'Inactive');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable2037 values(101,'Active');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable2037 values(101,'Active');
Query OK, 1 row affected (0.12 sec)

使用 select 語句從表中顯示所有記錄 −

mysql> select *from DemoTable2037;

這將生成以下輸出 −

+-----------+----------+
| StudentId | status   |
+-----------+----------+
| 99        | Active   |
| 99        | Inactive |
| 100       | Inactive |
| 99        | Active   |
| 100       | Inactive |
| 101       | Active   |
| 101       | Active   |
+-----------+----------+
7 rows in set (0.00 sec)

以下是用於選擇具有 ACTIVE 狀態的記錄的查詢 −

mysql> select StudentId,status
   -> from DemoTable2037 where status='Active'
   -> group by StudentId;

這將生成以下輸出 −

+-----------+--------+
| StudentId | status |
+-----------+--------+
| 99        | Active |
| 101       | Active |
+-----------+--------+
2 rows in set (0.00 sec)

更新於:07-Apr-2020

932 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.