[英]using Laravel Query builder to exclude certain string
Let us say I have a table named table
and it has the column called path
, the string format in the path
is like following让我们说我有一个表命名
table
,它有列叫做path
,在字符串格式path
是像以下
row path
1 /path1/aa/bg/c/da
2 /path1/ab/v
3 /path1/a/ba/ca
As you can see, each segment is separated by the char /
.如您所见,每个段都由 char
/
分隔。 How to format the condition in the where
using like
so that the only row returned is row 2
with 2 slashes
.如何使用
like
格式化where
的条件,以便返回的唯一行是row 2
带有2 slashes
。
I tried DB::table('table')->where('path','LIKE','/path1/%/%')->get()
我试过
DB::table('table')->where('path','LIKE','/path1/%/%')->get()
But the query above will return all the rows due to the last %
.但是由于最后一个
%
上面的查询将返回所有行。 How can I exclude /
after the last %
so that, the record which has the char /
after the last %
will not be included in the result ?我怎样才能排除
/
最后经过%
,这样,它具有字符记录/
后终于%
将不被包括在结果?
if you ar looking for match one character:如果您正在寻找匹配一个字符:
DB::table('table')->where('path','LIKE','/path1/_/_')->get()
"test%" matches all characters after “test%”匹配后面的所有字符
"%test" matches all characters before "%test" 匹配之前的所有字符
"_" matches one character “_”匹配一个字符
ELSE if the string lenght you are matching is variable, you can only use regular expressions, ELSE 如果您匹配的字符串长度是可变的,则只能使用正则表达式,
select * from table where path REGEXP '\/path1\/[a-zA-Z0-9_]+\/[a-zA-Z0-9_]+'
more generic match matching special characters:更通用的匹配特殊字符:
select * from table where path REGEXP '\/path1\/.+\/.+'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.