MySQL LIKE
The LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column. You can use the %
and _
wildcards to create patterns.
Examples with Tamil Kings
1. Using % Wildcard
SELECT * FROM tamil_kings_auto_increment
WHERE king_name LIKE 'Raja%';
Code Explanation: This query selects records where the king_name
starts with 'Raja'. The %
wildcard matches any sequence of characters.
2. Using _ Wildcard
SELECT * FROM tamil_kings_auto_increment
WHERE king_name LIKE 'K_rikala';
Code Explanation: This query selects records where the king_name
matches the pattern 'K_rikala'. The _
wildcard matches exactly one character.
Best Practices
- Use
%
to match any number of characters, including zero characters. - Use
_
to match exactly one character. - Be cautious when using wildcards at the beginning of a pattern, as it can impact performance.
Key Takeaways
- The
LIKE
operator is used to search for patterns in text. - Wildcards
%
and_
help create flexible search conditions.