首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql枚举不搜索没有引用值的记录

问Mysql枚举不搜索没有引用值的记录
EN

Stack Overflow用户
提问于 2018-02-14 11:25:24
回答 4查看 835关注 0票数 1

我有带有枚举的表域(‘0’,'1','2','3','4','5','6')。

在查询中,当我尝试使用In (1)时,它不工作,但当我使用IN ("1")时,它正在工作。

如果我用在(1,2)中,它是工作的。

因此,当" in“子句中只有一个值时,它应该可以不带引号。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-02-14 12:42:47

ENUM值被定义并存储为字符串。可以通过字符串值或数值索引值(从1开始,而不是从0开始)访问它们。

您的枚举值从'0‘开始,其数值索引值为1。因此,可以使用IN(1,2)从查询中获取一些数据,但这些数字将被视为索引,您实际上将得到数据返回记录,其中枚举值为'2’和'3‘(可能不是您认为要返回的数据)。

因为没有索引值为1 (emum值= '0')的数据,所以IN(1)不能工作,因为您的数据的枚举值为'1‘。

在一个实用示例http://sqlfiddle.com/#!9/3993b/1中使用此方法

或阅读有关在mysql文档中处理枚举文字的部分。

票数 2
EN

Stack Overflow用户

发布于 2018-02-14 11:39:40

您可以使用值作为字符串或整数,在()函数支持这两种类型。

IN运算符允许您在WHERE子句中指定多个值。

通常,IN()函数用于多个值,但它也支持单个值。对我来说,所有的查询都在工作。

代码语言:javascript
复制
SELECT * FROM `your_table` WHERE `id` IN('4') // String Number
SELECT * FROM `your_table` WHERE `id` IN('4','2','1') // String Multiple Number
SELECT * FROM `your_table` WHERE `id` IN(1) // Single Integer
SELECT * FROM `your_table` WHERE `id` IN(1,2,4) // Multiple Integer
票数 1
EN

Stack Overflow用户

发布于 2018-02-14 12:21:04

代码语言:javascript
复制
Avoid using number as 'enumeration values'

Doucumenation quotes-We strongly recommend that you do not use numbers as enumeration values, because it does not save on storage over the appropriate TINYINT or SMALLINT type, and it is easy to mix up the strings and the underlying number values (which might not be the same) if you quote the ENUM values incorrectly. If you do use a number as an enumeration value, always enclose it in quotation marks. If the quotation marks are omitted, the number is regarded as an index. See Handling of Enumeration Literals to see how even a quoted number could be mistakenly used as a numeric index value.'

Link- https://dev.mysql.com/doc/refman/5.7/en/enum.html#enum-limits

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48786040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档