首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MySQL在LOCATE内使用用户定义的变量时非法混合排序规则

MySQL在LOCATE内使用用户定义的变量时非法混合排序规则
EN

Stack Overflow用户
提问于 2019-06-05 03:21:43
回答 1查看 377关注 0票数 0

我有一个类似下面的查询,用于从表列中删除以特定子字符串开头和结尾的某些子串:

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'。据我所知,我的语法应该是正确的。我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 03:37:23

每个字符串文字都有一个字符集和一个排序规则。

对于简单语句SELECT ' string ',该字符串具有由character_set_connection和collation_connection系统变量定义的连接缺省字符集和排序规则。

字符串文字可以具有可选的字符集导入器和COLLATE子句,以将其指定为使用特定字符集和排序规则的字符串:

_charset_name‘’string‘排序collation_name

official documentation

来自该页面的示例:_utf8mb4'abc' COLLATE utf8mb4_danish_ci

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56450102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档