MySQL allows the use of regular expressions using REGEXP or RLIKE to perform advanced pattern matching. This approach is more flexible than the LIKE operator.
// Basic REGEXP syntax in MySQL
SELECT column_name
FROM table_name
WHERE column_name REGEXP 'pattern';
// Match names starting from A to M
SELECT *
FROM employees
WHERE name REGEXP '^[A-M]';
// Match product codes like 123-AB
SELECT *
FROM products
WHERE product_code REGEXP '^[0-9]{3}-[A-Za-z]{2}$';
// Exclude names starting with A to M
SELECT *
FROM employees
WHERE name NOT REGEXP '^[A-M]';
// Match only letters OR only numbers
SELECT *
FROM products
WHERE product_code REGEXP '^[A-Za-z]+$|^[0-9]+$';
The queries return rows only when the column value satisfies the given regular expression pattern.
^, $) for precise matchingBINARY for case-sensitive matches