Mysql查询查找子串

时间:2013-07-08 01:21:51

标签: mysql sql substring

我有一张这样的表:

country (id, name)

我想找到id,例如搜索字符串是“俄罗斯联邦”,在我的表中存储值“Russia”。

实际上,存储的值是搜索字符串的子字符串。

2 个答案:

答案 0 :(得分:4)

你可以这样试试:

SELECT *
  FROM Country
  WHERE 'Russian Federation' LIKE CONCAT('%', Name, '%')

这是SQL Fiddle演示。

答案 1 :(得分:1)

如果是这种情况,那么你可以这样做:

select *
from country
where concat(' ', SEARCHSTRING, ' ') like concat('% ', name, ' %')

或者您可以使用find_in_set()

select *
from country
where find_in_set(name, replace(SEARCHSTRING, ' ', ',')) > 0;