|
|
|
Custom Search
| |
|
| |
|
Using LIKE 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:
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. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. -- 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. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. -- 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. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. -- 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 In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. -- 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 In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
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. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 2 rows returned: Practice #7: Specify your own escape character In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set:
|
|
Copyright © 2012 GeeksEngine.com. All Rights Reserved. This website is hosted by LunarPages. 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 | Kung Fu Timer | Feedback | Terms of Use | Privacy Policy |