简体   繁体   中英

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') . The value got stored as 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 .

Enums, like all of SQL, use 1-based indexing, so for your enum index 1 is '1' and index 2 is '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.

See this SQLFiddle proving this.

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

该值需要指定为字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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