我可以比较 MySQL 枚举吗?
我有一个枚举: ENUM( 'alpha', 'beta', 'gamma', 'delta', 'omega' )
如果我按此列对表格进行排序,我会按照定义的正确顺序获得它们多于。
但是,我找不到选择其中子集的方法,例如 delta 之前的所有内容。 使用 WHERE status < 'delta' 仅返回 alpha 和 beta,而不返回 gamma。 看来MySQL使用的是字符串比较,而不是枚举索引比较。
我可以使用索引号 - 即 WHERE status < 4
- 但这有点代码味道(幻数),如果我在枚举中插入新值,可能会中断。
I have an enumeration: ENUM( 'alpha', 'beta', 'gamma', 'delta', 'omega' )
If I sort my table by this column I get them in the correct order defined above.
However, I can't find a way to select a subset of these, e.g. everything before delta. Using WHERE status < 'delta'
only returns alpha and beta, not gamma. It seems MySQL uses a string comparison, not enum index comparison.
I could use the index numbers - i.e. WHERE status < 4
- but it's a bit of a code smell (magic numbers) and may break if I insert new values into the enumeration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
status+0
返回 ENUM 的索引,从 1 开始。请参阅 http://serghei.net/docs/database/mysql/doc/E/N/ENUM.html
You can use
status+0
to return the index of the ENUM, starting from 1.Refer http://serghei.net/docs/database/mysql/doc/E/N/ENUM.html
您尝试对元数据使用数据操作方法,这肯定会很尴尬。
这是用查找表的外键替换
ENUM
的一个很好的理由。 然后您可以使用传统的数据操作技术。You're trying to use data-manipulation methods on metadata, and this is bound to be awkward.
This is a good reason to replace the
ENUM
with a foreign key to a lookup table. Then you can use conventional data-manipulation techniques.刚刚遇到同样的问题。 如果你想对枚举字段进行排序,你必须首先将其转换为字符串类型(类别是示例中我的枚举字段):
eazy!
just came across the same issue. if you want to sort a enum field you have to cast it to a string type first (category is my enum field in the example):
eazy!
您可以使用
FIELD(column, "string1 ", "string2", ...)
查找包含可能的ENUM
值的任何特定子集的行。如果要使用范围版本,可以使用
FIND_IN_SET("needle", "hay,stack")
返回索引,但您必须首先使用另一个查询从表定义中提取 ENUM 列表。You can use
FIELD(column, "string1", "string2", ...)
to find rows with any particular subset of possibleENUM
values.If you want to use the range version, you can use
FIND_IN_SET("needle", "hay,stack")
to return the index but you'll have to extract the ENUM list out of the table definition first with another query.创建一个函数:
然后在查询中使用它,如下所示:
SELECT REPLACE(REPLACE(REPLACE(COLUMN_TYPE,''', ''',','),'enum(',''),') ','') 将采用
COLUMN_TYPE
,例如ENUM( 'alpha', 'beta', 'gamma', 'delta', 'omega' )
并将其转换为逗号分隔的列表:'alpha, beta, gamma, delta, omega'
。 然后FIND_IN_SET(_val, _lst)
获取索引。您唯一需要注意的是如何定义 ENUM(项目之间有或没有空格)和最里面的 REPLACE(from_string 中有或没有空格)。
Create a function:
Then use it in a query as follows:
The
SELECT REPLACE(REPLACE(REPLACE(COLUMN_TYPE,''', ''',','),'enum(',''),')','')
will take theCOLUMN_TYPE
such asENUM( 'alpha', 'beta', 'gamma', 'delta', 'omega' )
and turn it into a comma-separated list:'alpha, beta, gamma, delta, omega'
. Then theFIND_IN_SET(_val, _lst)
gets the index.The only thing you need to be careful with is how you define the ENUMs (with or without spaces between the items) and the inner-most
REPLACE
(with or without a space in the from_string).