php - SELECT WHERE any value in csv in array -
suppose have array:
$city = ["toronto", "chicago", "potato"]; and table looks this:
table name: city_table id city 1 toronto, orlando 2 buffalo, toledo 3 orlando, tomato 4 potato, chicago, nanaimo is possible have query gives me result?
id city 1 toronto, orlando 4 potato, chicago, nanaimo i tried doesn't work:
$city_string = "'toronto', 'chicago', 'potato'"; select * city_table city in $city_string
to search comma-separated value in mysql use find_in_set. can search 1 item @ time, need call separately each name $city, , combine them and.
select * city_table find_in_set('$city[0]', city_table) or find_in_set('$city[1]', city_table) or find_in_set('$city[2]', city_table) in actual code should use implode() construct clause dynamically.
note should not have spaces after commas in table, find_in_set not work csv way you've written it.
however, better normalize schema don't have comma-separated value. find_in_set can't optimized index, have perform full table scan.
Comments
Post a Comment