简体   繁体   English

将1作为数字保存到ENUM('1','0')字段中,将值设置为0

[英]Saving 1 as number into a ENUM('1','0') field set value as 0

Just notice something weird while was saving 1 as integer into a ENUM('1', '0') . 只是注意到将1作为整数保存到ENUM('1', '0')有些奇怪。 The value got stored as 0. 该值存储为0。

UPDATE table SET `somefield` = 1 WHERE `id` = 1;

SELECT id, `somefield` WHERE id = 1;

id, somefield
1, 0

Is there any way to make it work? 有什么办法可以使其工作? I prefer to don't modify DB. 我宁愿不修改数据库。

Also, any information about why this happen and the field is not converted would be very appreciated 此外,非常感谢您了解为什么会发生这种情况以及字段未转换的任何信息

When you provide an integer instead of a string for the update value of an enum, it's interpreted as the index of the enum values, not the value . 当为枚举的更新值提供整数而不是字符串时,它将被解释为枚举值的索引 ,而不是value

Enums, like all of SQL, use 1-based indexing, so for your enum index 1 is '1' and index 2 is '0' . 像所有SQL一样,枚举使用基于1的索引,因此对于您的枚举,索引1为'1' ,索引2为'0' Anything else is an error. 其他都是错误。

That means your update statement should result in a '1' , not a zero, so either your table is not defined as you say, or your update statement is not as you say. 这意味着您的更新语句应以'1'而不是零产生,因此您的表未按您的定义进行定义,或者您的更新语句未按您的描述进行定义。

See this SQLFiddle proving this. 请参阅此SQLFiddle证明这一点。

UPDATE table SET `somefield` = '1' WHERE `id` = 1;

该值需要指定为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM