[英]How can I use RLIKE to effectively combine LIKE and IN, *and* escape regex characters where needed?
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. 我需要弄清楚如何在数据库中查询序列A或序列B或序列C ,但是由于种种限制,这很难导航。
The previous solution used LIKE
(eg SELECT * FROM Table WHERE Serial LIKE
) - this worked well for searching for all serials ( %
) or a specific serial ( Serial A
). 先前的解决方案使用
LIKE
(例如SELECT * FROM Table WHERE Serial LIKE
)-在搜索所有序列( %
)或特定序列( 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 . 但是,我很快发现
IN
和LIKE
不能在MySql中合并,并且LIKE
使用的通配符不够灵活,无法找到Serial A或Serial B或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. 我立即想到的是改用
RLIKE
但是某些序列被命名为B + 3
东西,并且必须逃避才能找到。 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... 在这里,我以为使用一种转义字符的方法,例如
preg_quote()
-但是其中存在系统结构方面的更多问题...
Select Constraints Module 选择约束模块
A combobox is populated by querying the database (eg SELECT DISTINCT Serial FROM Table
) from which the users can select a serial, and then submit the form. 通过查询数据库(例如
SELECT DISTINCT Serial FROM Table
)来填充组合框,用户可以从中选择一个序列,然后提交表单。 The selected serial is passed as a GET
value to the next page. 选定的序列作为
GET
值传递到下一页。
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
变量的内容作为参数传递给SQL查询,以在页面上生成结果。 $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 include
d and there is no GET
value, an error will be thrown). $serial
可以由View Results Module所属的页面填充,也可以由传递给该页面的GET
值填充(如果该模块在include
d之前尚未填充该值,并且没有GET
值,将会引发错误)。
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. 如果这是由用户传递的,则使用
RLIKE
和preg_quote()
来搜索序列似乎很简单。 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. preg_quote()
可以在$serial
作为参数传递之前使用,并且更新的( WHERE Serial RLIKE
)查询应该可以按预期工作。 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. 如果我将
$serial
指定为(Serial A | Serial B | Serial C)
则preg_quote()
将转义正则表达式字符并找不到它们。 But if I don't use preg_quote()
, serials such as B + 3
will not be found because regex characters are left unescaped. 但是,如果我不使用
preg_quote()
,将找不到诸如B + 3
序列,因为正则表达式字符未转义。
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.
谢谢。
转义子字符串,而不是最终的正则表达式。
"(" . preg_quote(serialA) . "|" . preg_quote(serialB) . "|" . preg_quote(serialC) . ")"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.