0
$string = "San Martin, Mendoza, Argentina"

How do I match Mendoza in this sting with like statement?

'San Martin,%'

But not sure how for Mendoza?

  • 3
    `LIKE '%Mendoza%'` (But never, ever store data as comma separated items.) – jarlh Jan 09 '17 at 13:33
  • Thanks but I need to match the entire or individual work with the comma at the end! – Ana Maria Garcia Jan 09 '17 at 13:34
  • Possible duplicate of [Is there a combination of "LIKE" and "IN" in SQL?](http://stackoverflow.com/questions/3014940/is-there-a-combination-of-like-and-in-in-sql) – chambo Jan 09 '17 at 13:35
  • But you have no comma after Argentina... How do you know if an item is first, middle or last? As I said, comma separated items are a mess - will only cause you lots of trouble. – jarlh Jan 09 '17 at 13:36
  • This is my query: $sql[] = 'address LIKE \'% '.$word.',%\' OR \''.$word.',%\' OR \'% '.$word.' %\' OR `address` LIKE \'% '.$word.'\' OR `address` LIKE \''.$word.' %\' OR `address` = \''.$word.'\' '; – Ana Maria Garcia Jan 09 '17 at 13:39

1 Answers1

1

If you want to select rows with Mendoza in $string you can do

LIKE '%Mendoza%';

If you also want to ignore lower/uppercase, you can do

LOWER( string ) LIKE  '%mendoza%'

% is a SQL wildcard, see http://www.w3schools.com/sql/sql_wildcards.asp

cringe
  • 13,401
  • 15
  • 69
  • 102
  • I think this is what I want: '% murcia,% – Ana Maria Garcia Jan 09 '17 at 13:36
  • 1
    The important parts are the `%` wildcards though. – cringe Jan 09 '17 at 13:38
  • What do you think about my query: $sql[] = 'address LIKE \'% '.$word.',%\' OR \''.$word.',%\' OR \'% '.$word.' %\' OR `address` LIKE \'% '.$word.'\' OR `address` LIKE \''.$word.' %\' OR `address` = \''.$word.'\' '; – Ana Maria Garcia Jan 09 '17 at 13:40
  • Hm, looks like you want to a bit more complex logic on the string. Maybe have a look at https://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can-access-item-x#2685 to find out how to split the string and work with the individual items. – cringe Jan 09 '17 at 13:43