我可以比较 MySQL 枚举吗?

发布于 2024-07-09 04:19:25 字数 361 浏览 5 评论 0原文

我有一个枚举: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南七夏 2024-07-16 04:19:25

您可以使用 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

¢好甜 2024-07-16 04:19:25

您尝试对元数据使用数据操作方法,这肯定会很尴尬。

这是用查找表的外键替换 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.

李白 2024-07-16 04:19:25

刚刚遇到同样的问题。 如果你想对枚举字段进行排序,你必须首先将其转换为字符串类型(类别是示例中我的枚举字段):

SELECT
CONVERT(category USING utf8) as str_category 
FROM
example
GROUP BY
str_category
ORDER BY 
str_category

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):

SELECT
CONVERT(category USING utf8) as str_category 
FROM
example
GROUP BY
str_category
ORDER BY 
str_category

eazy!

聊慰 2024-07-16 04:19:25

您可以使用 FIELD(column, "string1 ", "string2", ...) 查找包含可能的 ENUM 值的任何特定子集的行。

SELECT * FROM `table` WHERE FIELD(`enum_column`, "alpha", "delta", "et cetera");

如果要使用范围版本,可以使用 FIND_IN_SET("needle", "hay,stack") 返回索引,但您必须首先使用另一个查询从表定义中提取 ENUM 列表。

You can use FIELD(column, "string1", "string2", ...) to find rows with any particular subset of possible ENUM values.

SELECT * FROM `table` WHERE FIELD(`enum_column`, "alpha", "delta", "et cetera");

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.

叹沉浮 2024-07-16 04:19:25

创建一个函数:

CREATE fEnumIndex(_table VARCHAR(50), _col VARCHAR(50), _val VARCHAR(50))
RETURNS INT DETERMINISTIC
BEGIN
    DECLARE _lst VARCHAR(8192);
    DECLARE _ndx INT;

    SELECT REPLACE(REPLACE(REPLACE(COLUMN_TYPE,''', ''',','),'enum(',''),')','')
    FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND
    TABLE_NAME=_table AND COLUMN_NAME=_col INTO _lst;
    SET _ndx = FIND_IN_SET(_val, _lst);
    RETURN _ndx;
END

然后在查询中使用它,如下所示:

SELECT * FROM MyTable WHERE Status < fEnumIndex('MyTable','Status','delta') ;

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:

CREATE fEnumIndex(_table VARCHAR(50), _col VARCHAR(50), _val VARCHAR(50))
RETURNS INT DETERMINISTIC
BEGIN
    DECLARE _lst VARCHAR(8192);
    DECLARE _ndx INT;

    SELECT REPLACE(REPLACE(REPLACE(COLUMN_TYPE,''', ''',','),'enum(',''),')','')
    FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND
    TABLE_NAME=_table AND COLUMN_NAME=_col INTO _lst;
    SET _ndx = FIND_IN_SET(_val, _lst);
    RETURN _ndx;
END

Then use it in a query as follows:

SELECT * FROM MyTable WHERE Status < fEnumIndex('MyTable','Status','delta') ;

The SELECT REPLACE(REPLACE(REPLACE(COLUMN_TYPE,''', ''',','),'enum(',''),')','') will take the COLUMN_TYPE such as ENUM( 'alpha', 'beta', 'gamma', 'delta', 'omega' ) and turn it into a comma-separated list: 'alpha, beta, gamma, delta, omega'. Then the FIND_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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文