Here is a tutorial on how to use LIKE keyword for pattern matching in MySQL select query.
MySQL Pattern Matching based on characters:
MySQL provides its own standard pattern matching based on characters. We can retrieve data according to a particular condition, that may involve some character matching by using LIKE in the WHERE clause instead of = or <>.
Using Like Keyword:
You can retrieve the desired records from MySQL table having
- Starting from particular set of characters
- Containing particular set of characters
- Ending with particular set of characters
- Containing exactly of N numbers of characters
Understanding with examples:
Lets see every case with some intelligible examples before going any further with any explanation.
SELECT * FROM table WHERE column_1 LIKE character-pattern
Having the same table in the database
Records starting from particular set of characters:
Put a % sign at the very end of the set of characters to be started with.
Records Ending with particular set of characters:
Put a % sign right behind the the set of characters to be ended with.
Records containing particular set of characters:
Put a % sign at the both ends of set of characters to be contained in the record and you will be able to extract all the records containing them.
Records containing exactly N numbers of characters:
This is as simple as useful and interesting it is. Put N number of underscores ‘_’ inside the single commas and extract the records having exactly N number of characters