MySQL 正则表达式如何准确查询包含日文假名的字段?
本篇文章给大家分享《MySQL 正则表达式如何准确查询包含日文假名的字段?》,覆盖了数据库的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
mysql 正则表达式模糊查询日文假名
问题:如何使用 mysql 正则表达式查询包含日文平假名和片假名的字段,但目前的正则表达式查询结果不准确?
答案:
原先的正则表达式存在一定的局限性,这里提供一个函数来解决这个问题:
create definer=`wq19bar`@`%` function `jp_char_inside`(s text) returns int(11) begin declare h text; declare p integer; declare l integer; declare head text; declare utf_8 text; set h = hex(s); set p = 1; set l = length(h); while p <= l do set head = substr(h, p, 1); if head < '8' then set p = p + 2; else set utf_8 = substr(h, p, 6); if (utf_8 >= 'e38181' and utf_8 <= 'e3829e') then return 1; end if; if (utf_8 >= 'e382a1' and utf_8 <= 'e383be') then return 1; end if; set p = p + 6; end if; end while; return 0; end
使用方法:
SELECT * FROM table_name WHERE jp_char_inside(title) = 1;
到这里,我们也就讲完了《MySQL 正则表达式如何准确查询包含日文假名的字段?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注主机宝贝公众号,带你了解更多关于的知识点!