字符串比较

Exisi 2023-01-20 22:00:23
Categories: Tags:

 

 

 

函数

说明

LIKE

简单的模式匹配

NOT LIKE

简单模式匹配的否定

STRCMP()

比较两个字符串

 

 

 

LIKE / NOT LIKE

 

语法

expr LIKE pat [ESCAPE 'escape_char']

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

 

 

 

mysql> SELECT 'David!' LIKE 'David_';

-> 1

mysql> SELECT 'David!' LIKE '%D%v%';

-> 1

 

 

 

mysql> SELECT 'David!' LIKE 'David\_';

-> 0

mysql> SELECT 'David_' LIKE 'David\_';

-> 1

 

 

mysql> SELECT 'David_' LIKE 'David|_' ESCAPE '|';

-> 1

 

 

 

 

mysql> SELECT 'abc' LIKE 'ABC';

-> 1

mysql> SELECT 'abc' LIKE _utf8mb4 'ABC' COLLATE utf8mb4_0900_as_cs;

-> 0

mysql> SELECT 'abc' LIKE _utf8mb4 'ABC' COLLATE utf8mb4_bin;

-> 0

mysql> SELECT 'abc' LIKE BINARY 'ABC';

-> 0

 

 

mysql> SELECT 'ä' LIKE 'ae' COLLATE latin1_german2_ci;

+------------------------------------------------+

|'ä' LIKE 'ae' COLLATE latin1_german2_ci |

+------------------------------------------------+

|                                                                0 |

+------------------------------------------------+

mysql> SELECT 'ä' = 'ae' COLLATE

latin1_german2_ci;

+--------------------------------------------+

|'ä' = 'ae' COLLATE latin1_german2_ci |

+--------------------------------------------+

|                                                           1 |

+--------------------------------------------+

 

 

 

 

STRCMP()


语法

STRCMP(expr1,expr2)

示例

mysql> SELECT STRCMP('text', 'text2');         

-> -1

mysql> SELECT STRCMP('text2', 'text');         

-> 1

mysql> SELECT STRCMP('text', 'text');         

-> 0

 

 

示例

mysql> SET @s1 = _utf8mb4 'x' COLLATE utf8mb4_0900_ai_ci;

mysql> SET @s2 = _utf8mb4 'X' COLLATE utf8mb4_0900_ai_ci;

mysql> SET @s3 = _utf8mb4 'x' COLLATE utf8mb4_0900_as_cs;

mysql> SET @s4 = _utf8mb4 'X' COLLATE utf8mb4_0900_as_cs;

mysql> SELECT STRCMP(@s1, @s2), STRCMP(@s3, @s4);

+------------------------+--------------------------+

|STRCMP(@s1, @s2)   |  STRCMP(@s3, @s4)   |

+------------------------+--------------------------+

|                             0   |                                -1 |

+------------------------+--------------------------+

 

 

示例

mysql> SET @s1 = _utf8mb4 'x' COLLATE utf8mb4_0900_ai_ci;

mysql> SET @s2 = _utf8mb4 'X' COLLATE utf8mb4_0900_ai_ci;

mysql> SET @s3 = _utf8mb4 'x' COLLATE utf8mb4_0900_as_cs;

mysql> SET @s4 = _utf8mb4 'X' COLLATE utf8mb4_0900_as_cs;

-->

mysql> SELECT STRCMP(@s1, @s3);

ERROR 1267 (HY000):Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT)
and (utf8mb4_0900_as_cs,IMPLICIT) for operation 'strcmp'

mysql> SELECT STRCMP(@s1, @s3 COLLATE utf8mb4_0900_ai_ci);

+------------------------------------------------------------+

| STRCMP(@s1, @s3 COLLATE utf8mb4_0900_ai_ci) |

+------------------------------------------------------------+

|                                                                                 0 |

+------------------------------------------------------------+

 

 

来自 <https://dev.mysql.com/doc/refman/8.0/en/string-comparison-functions.html>