|
Custom Search
| |
|
Using LIKE Comparison Operator When you do not know the exact value to search for, you can use LIKE operator to perform searches on similar values. The similar values are given a pattern that has to be matched. The pattern matching operation is referred to as wildcard search. Read Prerequisites for this tutorial and practices if you haven't done so. Consider the following facts when using LIKE operator in a SELECT statement:
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. Practice #1: Case-insensitive search by using LIKE operator. Note that to get correct results for Practice #1 and #2, I temporarily changed product name Chai to chai. This is to facilitate the queries to illustrate the difference between case-insensitive and case-sensitive search. -- Query 1: Retrieve all products which product name begins with C The two queries above display all products which product name begins with character C, regardless of upper-case or lower-case C. Pattern matching character % represents any number of characters after character C or c.
Query result set - both Query 1 and Query 2 returned exact 9 rows: Practice #2: Case-sensitive search by using LIKE BINARY operator. -- Retrieve all products which product name begins with C The query above displays all products which product name begins with upper-case character C. Note that product chai is not in the result because it's product name begins with lower-case character c which is eliminated in the result by the clause BINARY.
Query result set - 8 rows returned. Product chai is not in the result: Practice #3: Use LIKE operator for lower-case character search. -- Retrieve all products which product name begins with The query above displays all products which product name begins with lower-case character c. Only product chai is returned.
Query result set - 1 row returned: Practice #4: Match a single character -- Retrieve all products which product name begins with Use pattern matching character _ (underscore) to match any single character.
Query result set - one row returned: Practice #5: Combine pattern matching characters in one query /*
Query result set - 10 rows returned: Practice #6: Using default escape character \ (backslash) Before execute this query, change product name Mishi Kobe Niku to Mishi Kobe_Niku and change Queso Cabrales to Queso_Cabrales. /*
Query result set - 2 rows returned: Practice #7: Specify your own escape character /*
Query result set: Other tutorials in this category 1. The Basic SELECT Statement |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |