使用 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)
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP