Danilo
Danilo

Reputation: 2036

Mysql query find substring

I have a table like this:

country (id, name)

I want to find the id where for example the search string is "Russian Federation" and in my table is stored the value "Russia".

Pratically the stored value is a substring of the search string.

Upvotes: 1

Views: 385

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

If that is the case, then you can do:

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

Or you can use find_in_set():

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

Upvotes: 1

KaeL
KaeL

Reputation: 3659

You can try it like this:

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

Here's the SQL Fiddle Demo.

Upvotes: 4

Related Questions