Home  >  Article  >  Database  >  How to write like statement in sql

How to write like statement in sql

下次还敢
下次还敢Original
2024-05-02 02:15:24642browse

The LIKE statement in SQL is used to match strings based on a pattern. It uses the % and _ wildcard characters to match zero or more characters and a single character respectively. The syntax of the LIKE statement is: SELECT * FROM table_name WHERE column_name LIKE 'pattern'.

How to write like statement in sql

LIKE statement in SQL

The LIKE statement is used in SQL to match strings based on patterns. This statement uses wildcard characters (% and _) to represent any character or any single character.

Syntax

<code>SELECT * FROM table_name WHERE column_name LIKE 'pattern';</code>

Wildcards

  • %: Match zero or more Characters
  • _: Match any single character

Usage

Find characters containing a specific string Record

<code>SELECT * FROM table_name WHERE column_name LIKE '%string%';</code>

Example: Find records containing the string "John"

<code>SELECT * FROM customers WHERE name LIKE '%John%';</code>

Find records starting with a specific string

<code>SELECT * FROM table_name WHERE column_name LIKE 'string%';</code>

Example: Find records starting with the string "Smith"

<code>SELECT * FROM customers WHERE name LIKE 'Smith%';</code>

Find records ending with a specific string

<code>SELECT * FROM table_name WHERE column_name LIKE '%string';</code>

Example: Find records ending with the string "Jones" Records

<code>SELECT * FROM customers WHERE name LIKE '%Jones';</code>

Find records that do not contain a specific string

You can use the NOT LIKE operator to find records that do not contain a specific string.

<code>SELECT * FROM table_name WHERE column_name NOT LIKE 'pattern';</code>

Example

Find records that do not contain the "A" character:

<code>SELECT * FROM table_name WHERE column_name NOT LIKE '%A%';</code>

The above is the detailed content of How to write like statement in sql. 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