Home  >  Article  >  Database  >  How to use like in mysql

How to use like in mysql

下次还敢
下次还敢Original
2024-04-29 03:42:141142browse

The LIKE operator is used for pattern matching in MySQL. It uses wildcards to match character sequences, including: %: matches zero or more characters. _: Matches a single character. [ ]: Matches any characters listed within square brackets.

How to use like in mysql

Usage of LIKE operator in MySQL

The LIKE operator is used for pattern matching in MySQL. It allows you to find values ​​that match a specific pattern, which can contain wildcard characters.

Wildcard:

  • %: Matches zero or more characters.
  • _: Matches a single character.
  • [list]: Matches any characters listed within square brackets.

Syntax:

<code class="sql">SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';</code>

Example:

Find all names starting with "John":

<code class="sql">SELECT name
FROM customers
WHERE name LIKE 'John%';</code>

Find all names containing the letter "a":

<code class="sql">SELECT name
FROM customers
WHERE name LIKE '%a%';</code>

Find all names starting with "John" and ending with "son":

<code class="sql">SELECT name
FROM customers
WHERE name LIKE 'John%son';</code>

Tips:

  • The wildcard characters % and _ can be used anywhere in the pattern.
  • You can use square brackets [ ] to create character ranges. For example, '[a-z]' matches any lowercase letter.
  • The LIKE operator is case-sensitive unless the BINARY keyword is used. For example, 'John%' matches "John", but 'john%' does not.
  • If the pattern contains wildcard characters, the wildcard characters should be escaped to prevent them from being interpreted as text. For example, '\%' matches "%" itself.

The above is the detailed content of How to use like 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