Home > Database > Mysql Tutorial > body text

How to use like in mysql

下次还敢
Release: 2024-04-29 03:42:14
Original
1120 people have browsed it

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>
Copy after login

Example:

Find all names starting with "John":

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

Find all names containing the letter "a":

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

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

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

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!

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!