Home

Thursday, March 26, 2020

Searching columns for patterns in MYSQL


There are many different ways to search a column for a pattern.  You can use string functions and operators or regular expressions.

Below are some (not all) of the different ways to search a column with phone numbers that contain the sequential 415 number.

To find numbers starting with 415:

SELECT PhoneNumber FROM ContactInformation WHERE PhoneNumber LIKE '415%';
SELECT PhoneNumber FROM ContactInformation WHERE LEFT(PhoneNumber,3) = '415';
SELECT PhoneNumber FROM ContactInformation WHERE PhoneNumber REGEXP '^415';
SELECT PhoneNumber FROM ContactInformation WHERE SUBSTRING(PhoneNumber,1,3) = '415';

To find numbers containing 415 in the number:

SELECT PhoneNumber FROM ContactInformation WHERE PhoneNumber LIKE '%415%';
SELECT PhoneNumber FROM ContactInformation WHERE PhoneNumber REGEXP '415';
SELECT PhoneNumber FROM ContactInformation WHERE PhoneNumber REGEXP '415?';

Percentage of Full Table Scans on MYSQL

Below is a useful script I use to determine what percentage of my queries in MYSQL are performing full table scans.  Limiting full table sca...