简体   繁体   English

MySQL Like 语句默认不区分大小写

[英]MySQL Like statement not case-insensitive by default

As far as I know, LIKE statements in MySQL are case-insensitive by default as shown here .据我所知,MySQL 中的 LIKE 语句默认情况下不区分大小写,如此处所示

In my case, it seems they are not, for instance SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%rob%' doesn't return anything, while SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%Rob%' returns a record whose "name" column is "Robert".就我而言,它们似乎不是,例如SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%rob%'不返回任何内容,而SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%Rob%'返回“name”列为“Robert”的记录。

I want SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%rob%' to return the record whose "name" column is "Robert".我希望SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%rob%'返回“name”列为“Robert”的记录。

SELECT * FROM users WHERE LOWER(CONCAT(name, ' ', surname)) LIKE '%rob%'

As far as I know, LIKE statements in MySQL are case-insensitive by default as shown here.据我所知,MySQL 中的 LIKE 语句默认情况下不区分大小写,如此处所示。

The information described in this article is not correct.本文中描述的信息不正确。 The case sensitivity in string operations depends on the collation.字符串操作中是否区分大小写取决于排序规则。

MySQL 8.0 Reference Manual / Character Sets, Collations, Unicode . MySQL 8.0 参考手册/字符集、排序规则、Unicode

 CREATE TABLE test (txt_ai_ci VARCHAR(255) COLLATE utf8mb4_0900_ai_ci, txt_as_cs VARCHAR(255) COLLATE utf8mb4_0900_as_cs); INSERT INTO test VALUES ('GREEN', 'GREEN'), ('Green', 'Green'), ('green', 'green');
 SELECT txt_ai_ci, txt_ai_ci LIKE 'Gr%', txt_as_cs, txt_as_cs LIKE 'Gr%', txt_as_cs COLLATE utf8mb4_0900_ai_ci LIKE 'Gr%' FROM test 
txt_ai_ci txt_ai_ci txt_ai_ci LIKE 'Gr%' txt_ai_ci LIKE 'Gr%' txt_as_cs txt_as_cs txt_as_cs LIKE 'Gr%' txt_as_cs LIKE 'Gr%' txt_as_cs COLLATE utf8mb4_0900_ai_ci LIKE 'Gr%' txt_as_cs 整理 utf8mb4_0900_ai_ci LIKE 'Gr%'
GREEN绿色 1 1 GREEN绿色 0 0 1 1
Green绿色的 1 1 Green绿色的 1 1 1 1
green绿色 1 1 green绿色 0 0 1 1

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM