doumuyu0837 2014-11-11 10:37
浏览 118
已采纳

如何在需要的地方使用RLIKE有效地组合LIKE和IN,*和*转义正则表达式字符?

I need to figure out how to query my database for Serial A or Serial B or Serial C, however there are a variety of constraints making this difficult to navigate.

The previous solution used LIKE (e.g. SELECT * FROM Table WHERE Serial LIKE) - this worked well for searching for all serials (%) or a specific serial (Serial A). However, I quickly found that IN and LIKE cannot be combined in MySql, and that the wildcards used by LIKE are not flexible enough to find Serial A or Serial B or Serial C.

(Note: The actual serials have a great deal of variation and no consistent convention - I'm aware that serials as simple as these basic examples could be found using the % wildcard)

My immediate thought was to use RLIKE instead - however some of the serials are named things such as B + 3, and cannot be found without escaping. Here, I thought to use a method to escape the characters, such as preg_quote() - however therein lies more problems with the structure of the system...


Select Constraints Module

A combobox is populated by querying the database (e.g. SELECT DISTINCT Serial FROM Table) from which the users can select a serial, and then submit the form. The selected serial is passed as a GET value to the next page.

View Results Module

The contents of a $serial variable are passed as a parameter to a SQL query to generate results on the page. $serial is either populated by the page the View Results Module is part of, or by the GET value passed to the page (if the value hasn't been populated by the page before the module is included and there is no GET value, an error will be thrown).


Its this structure that's confusing me a little. It seems like it would be pretty straightforward to use RLIKE and preg_quote() to search for a serial if this was passed by the user. preg_quote() can be used on $serial before it is passed as a parameter and the updated (WHERE Serial RLIKE) query should work as expected. However, I still can't figure out how to search for more than one serial.

If I specify $serial as (Serial A | Serial B | Serial C) then preg_quote() will escape the regex characters and not find them. But if I don't use preg_quote(), serials such as B + 3 will not be found because regex characters are left unescaped.


I'd very much appreciate any feedback on improving this question - I've tried to explain it as clearly as possible but the complexity of the context and constraints make it difficult. Thank you.

  • 写回答

1条回答 默认 最新

  • doushi8231 2014-11-11 11:30
    关注

    Escape the substrings, not the final regexp.

    "(" . preg_quote(serialA) . "|" . preg_quote(serialB) . "|" . preg_quote(serialC) . ")"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型