如何在MySQL中查询查找字符串中的第二个单词

问题描述 投票:-1回答:4

我希望得到通配符查询来搜索第二个单词

这里列数据很少

sea food
food restaurant
Chinese food

如果我搜索“foo”。它应该回来

sea food
Chinese food

我怎么能在MySQL中做到这一点

mysql regex wildcard sql-like
4个回答
0
投票

在你的SQL查询中使用regexp ..类似于

select * from table where col regexp '^[A-Za-z]+\s(foo).*$'

编辑

用过你的桌子..试试这个..

select id,col from food where col regexp '^[A-Za-z]+ (foo)'

1
投票

怎么样:-

select * from table where food_name like '% foo%'

0
投票

使用以下查询。这对你的情况有帮助。

select column-name-list from table-name 
where right(your-desired-column-name,4) = 'food';

0
投票

这是一种做法

select * from
test
where 
substring_index(food_name,' ',-1) like '%foo%'
AND 
(
   LENGTH(food_name) - LENGTH( substring_index(food_name,' ',-1)) > 0
 )
;

DEMO

© www.soinside.com 2019 - 2024. All rights reserved.