doujia1904 2013-09-03 12:32
浏览 41
已采纳

使用regex unicode进行mysql查询

I would like to make a mysql query to catch : أرأء

this char أ may be typed like: ( أ or إ or ا or آ )

so when type:

$SQL=" select * from work where title REGEX '[\\u0622|\\u0623|\\u0625|\\u0627][\\u0631][\\u0622|\\u0623|\\u0625|\\u0627][\\u0621]" 

it doesn't work, I think the syntax is not good

  • 写回答

2条回答 默认 最新

  • dongsonglian7303 2013-09-03 18:52
    关注

    MySQL does not have \u escapes. Try to include the raw Unicode character in the query string, and pass it to MySQL in a utf8 connection. How you might do that depends on what language and connector you are using to talk to MySQL. Best would be to pass the pattern string in a parameter from your language's native Unicode string type if you have one; for example in Python-MySQLdb I can just do:

    group= u'[أإاآ]'
    pattern= u'%sر%sء' % (chars, chars)
    connection.execute('SELECT * FROM work WHERE title REGEX %s', [pattern])
    

    (nb no pipe characters needed in a regex character group)

    If you really can't get Unicode down your connection at all, MySQL does have a non-standard binary string escape which you could use to get the characters in through another encoding:

    WHERE title REGEX 0x5bd8a3d8a5d8a7d8a25dd8b15bd8a3d8a5d8a7d8a25dd8a1 AS utf8  - hex-encoded UTF-8 encoded string
    

    Generally you want to avoid using REGEX because it means any index on the title column will be ineffective and a full table search will be forced.

    One alternative would be to do a WHERE title IN a list of all 16 possible strings that would match the expression.

    (The most performant approach would be to use a database collation which already treats all four characters as equal. I'm not aware of a collation that matches that sloppily though.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向