简体   繁体   中英

SQL Like query with reverse name return result

Here is my sql table,

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

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. To search the name column for both Due and John we can try:

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:

SELECT id, name, city
FROM yourTable
WHERE name REGEXP '[[:<:]](John|Due)[[:>:]]';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM