• Learning MySQL By Example
  • Introduction
  • 1. How to Retrieve Data From a Single Table
  • 2. How to Retrieve Data from Multiple Tables
  • 3. Using Functions
  • 4. How to Insert, Update, Delete Data in Tables
  • 5. Summary Queries and Aggregate Functions
  • 6. Working With Subqueries
  • 7. SQL Views
  • 8. SQL Indexes
  • Glossary
  • Index
  • Download
  • Translations
  • 1.3

    LIKE and REGEXP Operators

    LIKE and REGEXP Operators

    • The LIKE keyword is used with the WHERE clause.
    • The LIKE keyword and can use two symbols as wildcards. The percent ( % ) symbol matches any number of characters and the underscore ( _ ) matches a single character
    • REGEXP keyword allows you to do more complex pattern matching than a LIKE keyword/
    • Some version of REGEXP exists in many computer languages. Refer to the “LIKE and REGEXP” handout for a full list of examples.

    Table 2. LIKE Keyword

    LIKE Symbol Description

    %

    Match any string of characters to the left of the symbol

    _

    Match a single character

    Code Example:

    USE world;
    SELECT name
    FROM country
    WHERE name LIKE ‘A%’

    Results:

    LIKE.png

    Table 3. REXEXP Keyword

    REGEXP Characters Description

    ^

    Match the pattern to the beginning of the value being tested.

    $

    Match the pattern to the end of the value being tested.

    .

    Matches any single character.

    [charlist]

    Matches any single character listed within the brackets.

    [char1 – char2]

    Matches any single character within the given range.

    |

    Separates two string patterns and matches either one

    Code Example:

    USE world;
    SELECT name
    FROM country
    WHERE name REGEXP 'g[o,u]';

    Results:

    REGEXP.png

    This content is provided to you freely by EdTech Books.

    Access it online or download it at https://edtechbooks.org/learning_mysql/13_like_and_regexp_o.