Home > Database > SQL > body text

How to write like statement in sql

下次还敢
Release: 2024-05-02 02:15:24
Original
601 people have browsed it

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

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

Example: Find records containing the string "John"

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

Find records starting with a specific string

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

Example: Find records starting with the string "Smith"

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

Find records ending with a specific string

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

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

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

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

Example

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

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

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!

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!