我有一个类似下面的查询,用于从表列中删除以特定子字符串开头和结尾的某些子串:
UPDATE om_posts SET post_content=REPLACE(post_content, SUBSTRING(
post_content,
LOCATE(' style="', post_content),
LOCATE('"', post_content, LOCATE(' style="', post_content )+ 8) - LOCATE(' style="', post_content ) + 1
),'')
where post_type="post";
我想让它更好地可重用,所以我想把这些字符串抽象出来。我在mysql中遇到了用户定义的变量,并进行了如下重构:
SET @beginning = ' style="';
SET @ending ='"';
UPDATE om_posts SET post_content=REPLACE(post_content, SUBSTRING(
post_content,
LOCATE(@beginning, post_content),
LOCATE(@ending, post_content, LOCATE(@beginning, post_content )+ 8) - LOCATE(@beginning, post_content ) + 1
),'')
where post_type="post";
但这会给出一个错误:Error in query (1267): Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation 'locate'
。据我所知,我的语法应该是正确的。我遗漏了什么?
发布于 2019-06-05 03:37:23
每个字符串文字都有一个字符集和一个排序规则。
对于简单语句SELECT ' string ',该字符串具有由character_set_connection和collation_connection系统变量定义的连接缺省字符集和排序规则。
字符串文字可以具有可选的字符集导入器和COLLATE子句,以将其指定为使用特定字符集和排序规则的字符串:
_charset_name‘’string‘排序collation_name
来自该页面的示例:_utf8mb4'abc' COLLATE utf8mb4_danish_ci
https://stackoverflow.com/questions/56450102
复制相似问题