I am trying to execute this, but its throwing the error. Can anyone please help?
SELECT * FROM `country` WHERE name regex '^ab$';
1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘regex ‘^ab$’ LIMIT 0, 30′ at line 1
-
My motto is to get “united states of america” record while user
searches for any phrase likeunited states of/united states/states united/america united/america states
Can anyone please tell me how I can write a regular expression for this requirement.
SELECT * FROM `country` WHERE name regex '^united states$';
But Like is working fine with
SELECT * FROM `country` WHERE name like '%ab%';
Any help would be much appreciated.
In addition to what sp00m has noted, fairly sure your regex isn’t what you are looking for.
^and$match the beginning and end of line, respectively, so using these regexes:is effectively looking for an exact match on “ab” and “united states”. If you are looking for a query equivalent to
like '%ab%', remove the beginning and end of line characters, your regex simply being:I do wonder what was wrong with using a
LIKE '%ab%'though. Seems like the more typical method.Neither option, however, will make the regex
united statesor^united states$or using a like similarly match “america united” though