A simple comparison of LIKE and REGEXP in SQL

1. In SQL , LIKE and REGEXP are two different syntaxes used for pattern matching. They are used to match strings matching specific patterns in database queries.

1. LIKE is an operator used for fuzzy matching in SQL. It uses the wildcard character % to represent the position of any number of numeric character sequences (including empty character sequences).

For example:

SELECT * FROM table_name WHERE column_name LIKE 'AA%';

The above query will return all strings starting with the letters " A A".

Result column: *

Query table name: table_name

Query column name: column_name

Query conditions: LIKE 'A A %'

 

Another common wildcard for LIKE is _, which represents an arbitrary character. For example:

SELECT * FROM table_name WHERE column_name LIKE '_AA';

The above query will return a string of 1 arbitrary character and AA .

Result column: *

Query table name: table_name

Query column name: column_name

Query conditions: LIKE '_ AA '

 

2. REGEXP is a regular expression operator used for more complex pattern matching. It uses regular expression syntax to describe rules for matching patterns.

For example:

SELECT * FROM table_name WHERE column_name REGEXP '^[a-z]+$';

The above query will return a string containing only lowercase letters.

Result column: *

Query table name: table_name

Query column name: column_name

Query conditions: REGEXP '^[az]+$'

Note: Turning on case sensitivity may affect query performance and results.

 

3. Query the value containing the specified character (such as "AA" ) anywhere in the field

a.like %

SELECT * FROM table_name WHERE column_name LIKE '%AA%';

b. Use REGEXP

SELECT * FROM table_name WHERE column_name REGEXP 'AA';

like matches the entire field ; REGEXP matches within the field . If ^ and $ are added , it will match the entire field . If REGEXP ' AA ' is changed to REGEXP ' ^AA$ ' , only the characters with " AA " will be returned. string

 

Using REGEXP can achieve more flexible and complex pattern matching, such as using metacharacters, character classes, groupings, quantifiers, etc. These are features that LIKE cannot achieve.

It should be noted that different database management systems may have different support for LIKE and REGEXP. In some databases, it may be necessary to use specific functions or extensions to implement the functionality of REGEXP. Therefore, please refer to the documentation of the database used for specific syntax and usage.

 

 

2. In MySQL , LIKE and REGEXP are two operators used for pattern matching, but they have some differences in function and use.

1. Grammatical differences:

  • LIKE uses a simple wildcard syntax, where % represents any sequence of digits (including the empty character sequence) and _ represents any character.
  • REGEXP uses regular expression syntax, which is more flexible and powerful and can express more complex pattern matching rules.

2. Performance differences:

  • In general, the matching performance of the LIKE mode is relatively high because it uses index optimization in MySQL and can use the index for efficient search.
  • The matching performance of the REGEXP pattern is relatively low because it requires regular expression calculation, requires more computing resources and time, and is more computationally expensive than simple string matching.

3. Applicable scenarios:

  • LIKE is typically used for simple fuzzy string matching, such as matching strings that begin with a certain character or contain a specific pattern.
  • REGEXP is suitable for more complex pattern matching, such as matching specific character classes, repeating patterns, or other complex rules in regular expressions.

In short, in MySQL, both LIKE and REGEXP can be used for pattern matching, but their syntax, performance, and application scenarios are different. Generally speaking, in simple string fuzzy matching scenarios, using LIKE is more efficient; and when more complex pattern matching is required, using REGEXP is more flexible.

Which method to choose depends on the specific usage scenarios and needs.

Guess you like

Origin blog.csdn.net/xijinno1/article/details/133151867