MySQL程序。 LIKE 模式匹配 - 西里尔字母字符串
我在使用一个 MySQL 过程时遇到问题。该过程本身有效,但仅当我使用拉丁字符时才有效。如果我使用西里尔字母字符串,它就不起作用。该表位于页面顶部的 utf8_general_ci 中,我有“SET NAMES 'utf8'”。任何有关这篇文章的帮助都是值得赞赏的。下面是我的代码。
CREATE PROCEDURE `selUsers`(IN usrNme VARCHAR(25))
BEGIN
SELECT *
FROM users WHERE 1
AND (user_real_name LIKE CONCAT('%', usrNme, '%') OR user_company LIKE CONCAT('%', usrNme, '%') OR user_name LIKE CONCAT('%', usrNme, '%'))
ORDER BY user_real_name ASC LIMIT 0, 15;
END
I'm having trouble with one MySQL procedure. The procedure itself works but only when I use latin characters. If I use cyrillic string it doesn't work. The table is in utf8_general_ci on the top of the page I have "SET NAMES 'utf8'". Any help regarding this post is appreciated. Below is my code.
CREATE PROCEDURE `selUsers`(IN usrNme VARCHAR(25))
BEGIN
SELECT *
FROM users WHERE 1
AND (user_real_name LIKE CONCAT('%', usrNme, '%') OR user_company LIKE CONCAT('%', usrNme, '%') OR user_name LIKE CONCAT('%', usrNme, '%'))
ORDER BY user_real_name ASC LIMIT 0, 15;
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使它工作,你应该在打开连接并选择数据库后使用
SET NAMES
,告诉mysql客户端将使用特定的字符集发送数据。请查看此处了解详细信息。对于您的具体情况,应该是
另外,我会使用 utf8_unicode_ci 而不是 utf8_general_ci。确实,utf8_unicode_ci 比 utf8_general_ci 慢,但您会得到更好的比较(更准确)...utf8_general_ci 仅适用于西里尔文的俄语和保加利亚语子集。 白俄罗斯语、马其顿语、塞尔维亚语和乌克兰语中使用的额外字母未正确排序...您应该如果您要使用这些西里尔字母中的一些字母,请记住这一点。
In order to make it work, you should use
SET NAMES
after you open connection and select database, to tell mysql that client will use specific character set to send data. Check out here for details.For your specific case, it should be
Also, I would use utf8_unicode_ci instead of utf8_general_ci. It is true that utf8_unicode_ci is slower than utf8_general_ci, but you will get better comparison (more accurate)...utf8_general_ci is fine only for Russian and Bulgarian subset of Cyrillic. Extra letters used in Belarusian, Macedonian, Serbian, and Ukrainian are not sorted well...you should have that on mind if you will use some of the letters from those cyrilic alphabets.