使用 ENUM 选择 MySQL 中处于 ACTIVE 状态的记录
mysqlmysqli database更新于 2025/9/29 11:56:17
首先创建一个表。在这里,我们使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

