您可以借助INFORMATION_SCHEMA.COLUMNS表在MySQL数据库中获取可能的枚举值。语法如下-
SELECT COLUMN_TYPE AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ‘yourDatabaseName’ AND TABLE_NAME = 'yourTableName' AND COLUMN_NAME = 'yourEnumColumnName';
为了理解上述语法,让我们创建一个具有ENUM数据类型的表。创建表的查询如下-
mysql> create table EnumDemo -> ( -> Id int, -> Color ENUM('RED','GREEN','BLUE','BLACK','ORANGE') -> );
此处,表“ EnumDemo”存在于“样本”数据库中。现在,您可以实现上述语法,以从列中获取所有可能的枚举值。
查询如下-
mysql> SELECT -> COLUMN_TYPE as AllPossibleEnumValues -> FROM -> INFORMATION_SCHEMA.COLUMNS -> WHERE -> TABLE_SCHEMA = 'sample' AND TABLE_NAME = 'EnumDemo' AND COLUMN_NAME = 'Color';
输出结果
+---------------------------------------------+ | AllPossibleEnumValues | +---------------------------------------------+ | enum('RED','GREEN','BLUE','BLACK','ORANGE') | +---------------------------------------------+ 1 row in set (0.00 sec)