Mysql  LIKE Operator

Mysql LIKE Operator

·

2 min read

The Mysql-LIKE Operator

MySQL LIKE operator checks whether a specific character string matches a specified pattern. ... % is used to match any number of characters,

frequently I used this operator whenever I try to find something in my database i'll use this and get that result it's very useful for me.

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

The percent sign (%) represents zero, one, or multiple characters The underscore sign (_) represents one, the single character

LIKE Operator

SELECT * FROM YOUR_TABLE WHERE COLUMN_NAME LIKE 'a%'

here Find any values that start with "a"

after executing this query fetches data like this

  1. ahjk
  2. asd
  3. akl
  4. asdf
  5. aghjkl
  6. aaaa

same as like but before putting the % Finds any values that end with "a"

SELECT * FROM YOUR_TABLE WHERE COLUMN_NAME LIKE '%a'

example

  1. hjkaa
  2. qwertyaaa
  3. werthyjkaa
  4. oiuyaaa
  5. twerta
  6. lkja

Finds any values that have "or" in any position

SELECT * FROM YOUR_TABLE WHERE COLUMN_NAME LIKE '%or%'

here wherever data have or it's fetching

Finds any values that have "r" in the second position

SELECT * FROM YOUR_TABLE WHERE COLUMN_NAME LIKE '%_r%'

this like %_r% fetches data at the second position

Finds any values that start with "a" and are at least 2 characters in length

SELECT * FROM YOUR_TABLE WHERE COLUMN_NAME LIKE '%_r%'

Finds any values that start with "a" and are at least 3 characters in length

WHERE CustomerName LIKE 'a__%'

Finds any values that start with "a" and end with "o"

WHERE ContactName LIKE 'a%o'

That's all folks!

Did you find this article valuable?

Support ramu k by becoming a sponsor. Any amount is appreciated!