Home > Database > Mysql Tutorial > body text

Usage of % in mysql

下次还敢
Release: 2024-04-26 04:57:16
Original
743 people have browsed it

The % symbol in MySQL is a wildcard character used to match any sequence of characters in LIKE query conditions. Start a fuzzy match: % is placed at the beginning of a string to match any value that begins with that string. Ending fuzzy matching: % is placed at the end of a string to match any value that ends with that string. Match any character or string: % placed in the middle of a string matches any value that contains the string.

Usage of % in mysql

Usage of % in MySQL

The role of % symbol

The % symbol in MySQL is a wildcard character used to match any sequence of characters in the LIKE query condition.

Usage

In LIKE query conditions, you can use the % symbol to match the following types of data:

  • Start fuzzy matching :Placing % at the beginning of the search string means matching any value that begins with the string. For example: LIKE '%john' will match "john doe", "john smith", etc.
  • End fuzzy matching: Placing % at the end of the search string means matching any value that ends with the string. For example: LIKE 'smith%' will match "john smith", "jane smith", etc.
  • Any character or string matching: Placing % in the middle of the search string means matching any character or any value of the string that contains the string. For example: LIKE '%or%ger' will match "morgan", "orange", etc.

Examples

Here are some examples of fuzzy matching using the % wildcard:

  • SELECT * FROM users WHERE name LIKE '%john%'; Find all name column values ​​that contain the string "john".
  • SELECT * FROM products WHERE description LIKE '%apple%'; Find all description column values ​​that contain the string "apple".
  • SELECT * FROM orders WHERE order_id LIKE 'OR%'; Find all order_id column values ​​starting with "OR".

Note

  • % symbol can only be used in LIKE query conditions.
  • LIKE queries are case-sensitive, so when using the % wildcard character, make sure the search string matches the case of the value you need to match.
  • In order to improve query performance, it is recommended to use indexes, especially when the LIKE query condition contains the % wildcard character.

The above is the detailed content of Usage of % in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!