我有带有枚举的表域(‘0’,'1','2','3','4','5','6')。
在查询中,当我尝试使用In (1)时,它不工作,但当我使用IN ("1")时,它正在工作。
如果我用在(1,2)中,它是工作的。
因此,当" in“子句中只有一个值时,它应该可以不带引号。
发布于 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文档中处理枚举文字的部分。
发布于 2018-02-14 11:39:40
您可以使用值作为字符串或整数,在()函数支持这两种类型。
IN运算符允许您在WHERE子句中指定多个值。
通常,IN()函数用于多个值,但它也支持单个值。对我来说,所有的查询都在工作。
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发布于 2018-02-14 12:21:04
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
https://stackoverflow.com/questions/48786040
复制相似问题