I want to search for a word+number if that SQL returns nothing, I will default it to a full LIKE
search, is there any way to implement regex like this in SQL?
我想搜索一个单词+数字如果SQL什么都不返回,我将默认它为一个完整的搜索,有没有办法在SQL中实现这样的正则表达式?
Example:
例子:
$queryWord = potato
- It should first search for potato+somenumber up to 4 digits
- 它应该首先搜索最多4位数的马铃薯+somenumber
- It should return all matches with potato+somenumber
- 它应该返回所有匹配的马铃薯+somenumber
- If that fails, it defaults to a full search with wildcards on both sides
- 如果失败,它将默认为两边都有通配符的完整搜索
Like this:
是这样的:
$sql = "SELECT * FROM `words` WHERE `searchWord` LIKE '$queryWord+number';";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) == 0) {
$sql = "SELECT * FROM `words` WHERE `searchWord` LIKE '%$queryWord%';"; // full search
$result = mysqli_query($con, $sql);
// etc etc
}
1 个解决方案
#1
3
Just change your query to this:
只需将查询更改为以下内容:
$sql = "SELECT * FROM `words` WHERE `searchWord` REGEXP '$queryWord(\d{1,4})';";
and here is a Rubular that proves the Regex.
这是一个用来证明Regex的小叶状结构。
#1
3
Just change your query to this:
只需将查询更改为以下内容:
$sql = "SELECT * FROM `words` WHERE `searchWord` REGEXP '$queryWord(\d{1,4})';";
and here is a Rubular that proves the Regex.
这是一个用来证明Regex的小叶状结构。