Tutorial on using wildcards to filter in mysql
What are wildcards?
Wildcard characters are special characters used to match part of a value.
Search mode: Search conditions composed of literal values, wildcards, or a combination of both.
1.like operator:
All the operators introduced earlier are for constant values filtered. Whether it's matching or multiple values, testing greater or less than a known value, or checking a range of values, the common denominator is that the values used in filtering are known. However, this filtering method is not always easy to use. For example, how to search for all products whose product names contain the text anvil? It's impossible to use simple comparison operators, you must use wildcards. Use wildcards to create search patterns that compare specific data. In this example, if you want to find all products whose names contain anvil, you can construct a wildcard search pattern to find products where anvil appears anywhere in the product name.
To use wildcards in search clauses, the link operator must be used. like instructs MySQL, followed by a search pattern that uses wildcard matching instead of direct equality matching for comparison.
2. Percent (%) wildcard character
The most commonly used wildcard character is percent Number(%). In a search string, % represents any number of occurrences of any character. For example, to find all products that start with the word a, you can use the following select statement:
select prod_id, prod_name from products where prod_name like 'a%';
This example uses search Pattern 'a%'. When executing this sentence, search for any word starting with a. % tells MySQL to accept any character after a, no matter how many characters it has.
Note: Depending on how MySQL is configured, the search can be made case-sensitive. If case sensitive, 'a%' does not match the word Apache.
Wildcards can be used anywhere in the search pattern, and multiple wildcards can be used. The following example uses two wildcards, which are located at both ends of the pattern:
select prod_id, prod_name from products where prod_name like '%a%';
Code analysis: search pattern '%a% ' means match any value that contains the text anvil, regardless of what characters appear before or after it.
Wildcard characters can also appear in the middle of search patterns. For example, the following example:
select prod_namefrom products where prod_name like '%a%';
It is important to note that in addition to one or more characters, % can also match 0 character. % represents 0, 1 or more characters at a given position in the search pattern.
Note: 1. Spaces may interfere with wildcard matching. For example, when there are one or more spaces after a in '%a%', the clause where prod_name like '%a%'; will not match them because there are extra characters after a. A simple way to solve this problem is to add a % at the end of the search pattern. Another better way is to use a function to remove the trailing spaces.
2. Although it seems that the % wildcard can match anything, there is one exception, which is NULL. Even where prod_name like ‘%’ does not match rows with the value NULL as the product name.
3. Underscore '_' wildcard
Another useful wildcard character is the underscore (_ ). Underscore has the same purpose as %, but it matches a single character instead of multiple characters.
For example:
select prod_id,prod_name from products where products where prod_name like '_ ton anvil';
Output:
The search pattern in this where clause gives two wildcard characters followed by text. The results only display lines that match the search pattern: the underline in the first line matches 1, and the underline in the second line matches 2.
You can try using % to match the results returned and compare them.
The above is the detailed content of Tutorial on using wildcards to filter in mysql. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Python implements XML data filtering and filtering. XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. It is flexible and scalable and is often used for data exchange between different systems. When processing XML data, we often need to filter and filter it to extract the information we need. This article will introduce how to use Python to filter and filter XML data. Import the required modules Before starting, we

When using Quark Browser, there is a function to filter duplicate files. Some friends are not very familiar with this. Here I will introduce how to turn on this function. If you are interested, come and take a look with me. 1. First, click "Quark Browser" on your mobile phone to enter the interface, then click and select "Quark Network Disk" in the options in the middle of the page to open and enter. 2. Find "Backup Settings" in the lower part of the Quark network disk interface, and click to open it, as shown in the figure below: 3. Next, on the page you enter, there is a "Filter Duplicate Files", which is displayed behind it There is a switch button. Click the circular slider on it and set it to color to turn on this function. When you continue to back up files, duplicate files will be skipped to save network disk capacity.

How to use PHP functions to search and filter data? In the process of developing using PHP, it is often necessary to search and filter data. PHP provides a wealth of functions and methods to help us achieve these operations. This article will introduce some commonly used PHP functions and techniques to help you search and filter data efficiently. String search Commonly used string search functions in PHP are strpos() and strstr(). strpos() is used to find the position of a certain substring in a string. If it exists, it returns

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

PHP and PHPMAILER: How to implement automatic filtering of mail sending? In modern society, email has become one of the important ways for people to communicate. However, with the popularity and widespread use of email, the amount of spam has also shown an explosive growth trend. Spam emails not only waste users' time and network resources, but may also bring viruses and phishing behaviors. Therefore, when developing the email sending function, it becomes crucial to add the function of automatically filtering spam. This article will introduce how to use PHP and PHPMai

PHP Data Filtering: Processing Date and Time Input Overview: When developing web applications, it is often necessary to process date and time data entered by the user. Since user input may contain various formats and errors, effective data filtering and validation are necessary to ensure data accuracy and security. This article explains how to use PHP to handle date and time input, and provides corresponding code examples. Filtering and validation principles: Before processing date and time inputs, you first need to determine the corresponding filtering and validation principles. Here are some common ones

PHP Data Filtering: How to Prevent File Upload Vulnerabilities The file upload function is very common in web applications, but it is also one of the most vulnerable to attacks. Attackers may exploit file upload vulnerabilities to upload malicious files, leading to security issues such as server system intrusion, user data being leaked, or malware spreading. In order to prevent these potential threats, we should strictly filter and inspect files uploaded by users. Verify file type An attacker may rename the .txt file to a .php file and upload

Oracle wildcard characters include "%", "_", "[]" and "[^]". Detailed introduction: 1. The wildcard character "%" means matching any character, including zero characters. Using the wildcard character "%" in Oracle can implement fuzzy query. When the wildcard character "%" is used in the query statement, the query will return all characters matching the specified character. Pattern matching string; 2. The wildcard character "_" means matching any single character. In Oracle, the wildcard character "_" can be used to achieve exact matching. When using wildcard characters in query statements, etc.
