我有一个包含253个字符的字符串,但是由于某些字符超过一个字节(在本例中,它包含几个零宽度的空格),它占用了261个字节。
我想把这个字符串插入一个 VARCHAR(255)
a列 utf8mb4
表,但它抛出以下错误:
INSERT INTO mytable(mycolumn) VALUES("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.")
/*Error 1406: Data too long for column*/
下面是两个长度函数输出的示例:
/* Returns 253 */
SELECT CHAR_LENGTH("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.");
/* Returns 261 */
SELECT LENGTH("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.");
要解决这个问题,我需要删掉足够的字符,使字符串在字节限制内。
不幸的是 LEFT()
函数似乎不适用于此:
/* Still returns 261 because it counts characters, not bytes */
SELECT LENGTH(LEFT("This string contains 253 characters which should fit inside of a varchar(255) column, but because of the zero width spaces, it takes up more than 255 bytes and therefore throws an error when you try to insert it into the table and that makes me sad.", 255));
那么,我怎样才能把绳子剪下来呢?
1条答案
按热度按时间u4dcyp6a1#
一种解决方法是使用另一种语言提前处理字符串。
例如,这里有一个python的工作解决方案:
也就是说,如果一个纯mysql解决方案存在的话,它会更好。