LIKE (2024)

Syntax

expr LIKE pat [ESCAPE 'escape_char']expr NOT LIKE pat [ESCAPE 'escape_char']

Contents

  1. Syntax
  2. Description
  3. Examples
  4. Optimizing LIKE
  5. See Also

Description

Tests whether expr matches the pattern pat. Returns either 1 (TRUE) or 0 (FALSE).Both expr and pat may be any valid expression and are evaluated to strings.Patterns may use the following wildcard characters:

  • % matches any number of characters, including zero.
  • _ matches any single character.

Use NOT LIKE to test if a string does not match a pattern. This is equivalent to usingthe NOT operator on the entire LIKE expression.

If either the expression or the pattern is NULL, the result is NULL.

LIKE performs case-insensitive substring matches if the collation for theexpression and pattern is case-insensitive. For case-sensitive matches, declare either argumentto use a binary collation using COLLATE, or coerce either of them to a BINARYstring using CAST. Use SHOW COLLATION to get a list ofavailable collations. Collations ending in _bin are case-sensitive.

Numeric arguments are coerced to binary strings.

The _ wildcard matches a single character, not byte. It will only match a multi-byte characterif it is valid in the expression's character set. For example, _ will match _utf8"€", but itwill not match _latin1"€" because the Euro sign is not a valid latin1 character. If necessary,use CONVERT to use the expression in a different character set.

If you need to match the characters _ or %, you must escape them. By default,you can prefix the wildcard characters the backslash character \ to escape them.The backslash is used both to encode special characters like newlines when a string isparsed as well as to escape wildcards in a pattern after parsing. Thus, to match anactual backslash, you sometimes need to double-escape it as "\\\\".

To avoid difficulties with the backslash character, you can change the wildcard escapecharacter using ESCAPE in a LIKE expression. The argument to ESCAPEmust be a single-character string.

Examples

Select the days that begin with "T":

CREATE TABLE t1 (d VARCHAR(16));INSERT INTO t1 VALUES ("Monday"), ("Tuesday"), ("Wednesday"), ("Thursday"), ("Friday"), ("Saturday"), ("Sunday");SELECT * FROM t1 WHERE d LIKE "T%";
SELECT * FROM t1 WHERE d LIKE "T%";+----------+| d |+----------+| Tuesday || Thursday |+----------+

Select the days that contain the substring "es":

SELECT * FROM t1 WHERE d LIKE "%es%";
SELECT * FROM t1 WHERE d LIKE "%es%";+-----------+| d |+-----------+| Tuesday || Wednesday |+-----------+

Select the six-character day names:

SELECT * FROM t1 WHERE d like "___day";
SELECT * FROM t1 WHERE d like "___day";+---------+| d |+---------+| Monday || Friday || Sunday |+---------+

With the default collations, LIKE is case-insensitive:

SELECT * FROM t1 where d like "t%";
SELECT * FROM t1 where d like "t%";+----------+| d |+----------+| Tuesday || Thursday |+----------+

Use COLLATE to specify a binary collation, forcingcase-sensitive matches:

SELECT * FROM t1 WHERE d like "t%" COLLATE latin1_bin;
SELECT * FROM t1 WHERE d like "t%" COLLATE latin1_bin;Empty set (0.00 sec)

You can include functions and operators in the expression to match. Select datesbased on their day name:

CREATE TABLE t2 (d DATETIME);INSERT INTO t2 VALUES ("2007-01-30 21:31:07"), ("1983-10-15 06:42:51"), ("2011-04-21 12:34:56"), ("2011-10-30 06:31:41"), ("2011-01-30 14:03:25"), ("2004-10-07 11:19:34");SELECT * FROM t2 WHERE DAYNAME(d) LIKE "T%";
SELECT * FROM t2 WHERE DAYNAME(d) LIKE "T%";+------------------+| d |+------------------+| 2007-01-30 21:31 || 2011-04-21 12:34 || 2004-10-07 11:19 |+------------------+3 rows in set, 7 warnings (0.00 sec)

Optimizing LIKE

  • MariaDB can use indexes for LIKE on string columns in the case where the LIKE doesn't start with % or _.
  • Starting from MariaDB 10.0, one can set the optimizer_use_condition_selectivity variable to 5. If this is done, then the optimizer will read optimizer_selectivity_sampling_limit rows to calculate the selectivity of the LIKE expression before starting to calculate the query plan. This can help speed up some LIKE queries by providing the optimizer with more information about your data.

See Also

Comments loading...

LIKE (2024)
Top Articles
What is required for evaluating large deposits?
What Are Treasurys? Government Bonds vs. Notes vs. Bills - NerdWallet
Cranes For Sale in United States| IronPlanet
Team 1 Elite Club Invite
Overnight Cleaner Jobs
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Green Bay Press Gazette Obituary
Bhad Bhabie Shares Footage Of Her Child's Father Beating Her Up, Wants Him To 'Get Help'
ds. J.C. van Trigt - Lukas 23:42-43 - Preekaantekeningen
Vocabulario A Level 2 Pp 36 40 Answers Key
Craigslist/Phx
Hallelu-JaH - Psalm 119 - inleiding
ATV Blue Book - Values & Used Prices
Robert Malone é o inventor da vacina mRNA e está certo sobre vacinação de crianças #boato
Classroom 6x: A Game Changer In The Educational Landscape
Learn2Serve Tabc Answers
Tnt Forum Activeboard
Po Box 35691 Canton Oh
Spoilers: Impact 1000 Taping Results For 9/14/2023 - PWMania - Wrestling News
Pekin Soccer Tournament
E22 Ultipro Desktop Version
Craigslistjaxfl
50 Shades Of Grey Movie 123Movies
Keurig Refillable Pods Walmart
Trivago Sf
Sullivan County Image Mate
Pirates Of The Caribbean 1 123Movies
Everything To Know About N Scale Model Trains - My Hobby Models
Kroger Feed Login
Restaurants In Shelby Montana
Catchvideo Chrome Extension
Trinket Of Advanced Weaponry
Mosley Lane Candles
Spy School Secrets - Canada's History
Litter-Robot 3 Pinch Contact & DFI Kit
Breckie Hill Fapello
Myql Loan Login
Stafford Rotoworld
Albertville Memorial Funeral Home Obituaries
Ukraine-Krieg - Militärexperte: "Momentum bei den Russen"
Florida Lottery Claim Appointment
Lucifer Morningstar Wiki
Truck Works Dothan Alabama
Cch Staffnet
N33.Ultipro
Accident On 40 East Today
Wpne Tv Schedule
Rheumatoid Arthritis Statpearls
Latina Webcam Lesbian
Round Yellow Adderall
Tamilyogi Cc
Elizabethtown Mesothelioma Legal Question
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5898

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.