In MySQL, you can use the LIKE operator in the WHERE clause of a SELECT statement to search for specific patterns in a column.
To select rows that start with a certain string, you can use the LIKE operator along with the ‘%’ wildcard. For example, to select all rows where the ‘name’ column starts with ‘SOMETHING’:
Copy codeSELECT * FROM your_table
WHERE name LIKE 'SOMETHING%';
To select rows that contain a certain string, you can use the LIKE operator along with the ‘%’ wildcard before and after the string. For example, to select all rows where the ‘name’ column contains ‘SOMETHING’:
Copy codeSELECT * FROM your_table
WHERE name LIKE '%SOMETHING%';
You can also use the CONCAT() function to concatenate the string with the wildcard characters before and after the string.
Copy codeSELECT * FROM your_table
WHERE name LIKE CONCAT('%','SOMETHING','%');
If you want to select the rows that first starts with ‘SOMETHING’ and then contains ‘SOMETHING’, you can use the following query:
Copy codeSELECT * FROM your_table
WHERE name LIKE 'SOMETHING%' AND name LIKE '%SOMETHING%';
It will give you the rows which first starts with ‘SOMETHING’ and then contains ‘SOMETHING’ in the column ‘name’
Last modified: March 3, 2023