简体   繁体   中英

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 .

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".

I want SELECT * FROM users WHERE CONCAT(name, ' ', surname) LIKE '%rob%' to return the record whose "name" column is "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.

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 .

 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 LIKE 'Gr%' txt_as_cs txt_as_cs LIKE 'Gr%' txt_as_cs COLLATE utf8mb4_0900_ai_ci LIKE 'Gr%'
GREEN 1 GREEN 0 1
Green 1 Green 1 1
green 1 green 0 1

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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