MySQL 查询选择 ENUM('M', 'F') 作为 'Male' 或 'Female'?
mysqlmysqli database更新于 2025/9/3 20:28:17
您可以使用 IF() 来实现这一点。让我们首先创建一个表。这里的其中一列具有 ENUM 类型
mysql> create table DemoTable
(
UserId int,
UserName varchar(40),
UserGender ENUM('M','F')
);
Query OK, 0 rows affected (1.11 sec)
使用 insert 命令在表中插入记录 −
mysql> insert into DemoTable values(1,'John','M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(2,'Maria','F'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(3,'David','M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(4,'Emma','F'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 1 | John | M | | 2 | Maria | F | | 3 | David | M | | 4 | Emma | F | +--------+----------+------------+ 4 rows in set (0.00 sec)
以下查询选择 ENUM('M', 'F') 作为 'Male' 或 'Female' −
mysql> SELECT UserId,UserName,IF(UserGender='F','Female', 'Male') AS `UserGender` from DemoTable;
这将产生以下输出 −
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 1 | John | Male | | 2 | Maria | Female | | 3 | David | Male | | 4 | Emma | Female | +--------+----------+------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

