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 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图