Question

THere are 2 forms of pattern matching in MySQL.

  1. SQL Pattern matching.
  2. Extended Regex pattern matching

Can anyone please specify the exact difference between these 2 forms of pattern matching? I have googled it a lot, but could'nt get a convincing answer. The official docs also couldnt give a good answer. Any help will be greatly appreciated

Thanks in Advance

Was it helpful?

Solution

SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. You do not use = or <> when you use SQL patterns; use the LIKE or NOT LIKE comparison operators instead.

One important difference between the LIKE and REGEXP operators is that the LIKE operator only returns True if the pattern matches the whole string. E.g. WHERE testcolumn LIKE 'jg' will return only rows where testcolumn is identical to jg, except for differences in case perhaps. On the other hand, WHERE testcolumn REGEXP 'jg' will return all rows where testcolumn has jg anywhere in the string. Use WHERE testcolumn REGEXP '^jg$' to get only columns identical to jg. The equivalent of WHERE testcolumn LIKE 'jg%' would be WHERE testcolumn REGEXP '^jg'. There's no need to put a .* at the end of the regex (the REGEXP equivalent of LIKE's %), since partial matches are accepted.

http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top