![]() We’ve walked you through several examples, in which you’ve learned: As you’ve seen, this operator can be used to retrieve values from textual columns which match a given pattern. In this post, we’ve covered the MySQL LIKE operator. Test out the LIKE operator in the sandbox below: Let’s rewrite the query to retrieve match the last name, but specify the “#” character as the escape character: SELECT * FROM students WHERE last_name LIKE '#_%' ESCAPE '#' Code language: SQL (Structured Query Language) ( sql )Īnd the result: + -+-+-+ Actually, you can pick your own escaping character using the ESCAPE instruction. As you can see above, it works like a charm.įinally, even though the backslash is the default escaping character, it’s not the only one. Since the student’s name ends with the percent character, first we include the wildcard and then the actual percentage character we’re trying to match, preceded by the escape character. Now, for the first name: mysql> SELECT * FROM students WHERE first_name LIKE '%\%' As you can see, adding the backslash tells MySQL to treat the underscore just like a normal character. + -+-+-+ Code language: SQL (Structured Query Language) ( sql ) In short, you’d typically use this operator when you need to retrieve potentially many rows that all have something in common, and such rows can’t be obtained through the use of the comparison operators ( >, SELECT * FROM students WHERE last_name LIKE '\_%'
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |