Mysql WHERE column is in row

I hope this is not a very stupid question, but after a few hours I am looking for a solution, but to no avail.

I have a table with the name of the city:

cities.name

Berlin
New York
Hamburg
...

And I have a line: "Bonn New York Chicago"

Now I would like to select all the "famous" cities from this row. But, as you can see, I cannot split this string into an array (because of "New York"). Therefore, "FIND_IN_SET" does not suit me. The "LIKE" function works in the "wrong direction".

I need something like% cities.name% EKIL $ string: -D

Does anyone have an idea how I can deal with this problem without using PHP?

I hope I do not miss the forest for the trees ...

Many thanks for your help!

Tobias

+5
source share
1 answer

You need to make one cities array, and then check your string on your cities array.

 $to_match = array('Bonn','New York','Chicago'); // all cities array $str = "Bonn New York Chicago"; // Your city string $new_array = array(); foreach($to_match as $value) { if(stristr($str, $value)) { $new_array[] = $value; } } print_r($new_array); // here you can get only match cities against all cities array 

This will repeat each array element and then check if the match value exists in $str , then it will add it to $new_array .

+1
source

Source: https://habr.com/ru/post/1241875/


All Articles