Home  >  Article  >  Daily Programming  >  What does % represent in mysql

What does % represent in mysql

下次还敢
下次还敢Original
2024-04-27 03:42:14825browse

The percent symbol % in MySQL is a wildcard character used to match any number of characters in a string. It can be placed at the beginning, end, or middle to find strings that contain, begin, or end the specified character pattern. In addition to %, MySQL supports other wildcard characters such as underscore _ and square brackets [set].

What does % represent in mysql

The meaning of % in MySQL

In MySQL, the % symbol represents a wildcard character, which is used in strings Matches any number of characters.

Usage of wildcard characters

% symbol can be placed at the beginning, end, or middle of a string query to match various character patterns:

  • Matches starting with: %keyword matches any string starting with "keyword".
  • End match: keyword% matches any string ending with "keyword".
  • Intermediate matching: %keyword% matches any string containing "keyword".

Example

Here are some examples of MySQL queries using the % wildcard:

  • Find the query containing All user names for "John":

    <code class="sql">SELECT username FROM users WHERE username LIKE '%John%';</code>
  • Find all tables starting with "admin":

    <code class="sql">SHOW TABLES LIKE 'admin%';</code>
  • Find all fields that contain "customer" but end with "cust":

    <code class="sql">SELECT column_name FROM information_schema.columns WHERE column_name LIKE '%customer' AND column_name LIKE 'cust%';</code>

Other wildcards

In addition to %, MySQL also supports other wildcard characters:

  • _: The underscore matches a single character.
  • [set]: Square brackets match any character in the specified character set (for example, [a-z] matches all lowercase letters).

Notes

  • % Wildcard characters may cause query performance degradation, so they should be used with caution.
  • Case Sensitive: MySQL wildcards are case sensitive, which means that %keyword and %KEYWORD will not match the same results.
  • Escape characters: If you need to search for actual % characters in the query, use the escape character \, for example \%keyword.

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

Statement:
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