[英]SQL Like query with reverse name return result
Here is my sql table, 这是我的sql表,
id | name | city
------------------------
1 | John Due | New york
2 | Luke Wright | Kingston upon Hull
In my search inbox, If I search "Upon Kingston Hull" return second row or if I search "Due John" then return first row. 在我的搜索收件箱中,如果我搜索“在金斯顿赫尔”返回第二行或如果我搜索“到期约翰”然后返回第一行。
then how can i write like query ? 那我怎么写像查询? and also write query in Laravel Eloquent
并在Laravel Eloquent中编写查询
If you have a long term need for this requirement, you might want to look into full text search . 如果您长期需要此要求,则可能需要查看全文搜索 。 Short of this, MySQL offers a
REGEXP
operator which can do some fairly complex regex searching. REGEXP
,MySQL提供了一个REGEXP
运算符,它可以执行一些相当复杂的正则表达式搜索。 To search the name
column for both Due
and John
we can try: 要搜索
Due
和John
的name
列,我们可以尝试:
SELECT id, name, city
FROM yourTable
WHERE name REGEXP '[[:<:]]John[[:>:]]' AND name REGEXP '[[:<:]]Due[[:>:]]';
To search for Due
or John
, then we can use an alternation: 要搜索
Due
或 John
,我们可以使用替换:
SELECT id, name, city
FROM yourTable
WHERE name REGEXP '[[:<:]](John|Due)[[:>:]]';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.