Bug #39808 LIKE .. ESCAPE .. not behaving as expected
| Submitted: | 2 Oct 2008 12:49 | Modified: | 2 Oct 2008 19:57 |
|---|---|---|---|
| Reporter: | Jaka JanÄar | Email Updates: | |
| Status: | Verified | Impact on me: | |
| Category: | MySQL Server: Charsets | Severity: | S3 (Non-critical) |
| Version: | 5.0.67, 4.1, 5.0, 5.1, 6.0 bzr | OS: | Any |
| Assigned to: | Assigned Account | CPU Architecture: | Any |
[2 Oct 2008 12:49]
Jaka JanÄar
[2 Oct 2008 17:40]
Sveta Smirnova
Thank you for the report. Please provide output of SELECT name FROM companies WHERE name LIKE '%XXX%' ESCAPE 'X';
[2 Oct 2008 18:56]
Jaka JanÄar
After some more research, it seems to be a charset/collation problem. If I use utf8_general_ci instead of utf8_unicode_ci it works as expected. Here's a complete test:
# utf_unicode_ci
################
mysql> SET NAMES utf8 COLLATE utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DROP TABLE likeTest;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE likeTest (
-> string VARCHAR(255)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> INSERT INTO likeTest VALUES ('I\'m a string with an X!');
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> SELECT string FROM likeTest WHERE string LIKE '%XX%' ESCAPE 'X';
Empty set (0.00 sec)
mysql> SELECT string FROM likeTest WHERE string LIKE '%XXX%' ESCAPE 'X';
+-------------------------+
| string |
+-------------------------+
| I'm a string with an X! |
+-------------------------+
1 row in set (0.00 sec)
# utf_general_ci
################
mysql> SET NAMES utf8 COLLATE utf8_general_ci;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> DROP TABLE likeTest;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE likeTest (
-> string VARCHAR(255)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> INSERT INTO likeTest VALUES ('I\'m a string with an X!');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT string FROM likeTest WHERE string LIKE '%XX%' ESCAPE 'X';
+-------------------------+
| string |
+-------------------------+
| I'm a string with an X! |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT string FROM likeTest WHERE string LIKE '%XXX%' ESCAPE 'X';
Empty set (0.00 sec)
[2 Oct 2008 19:57]
Sveta Smirnova
Thank you for the report. Verified as described.
[24 Nov 2008 14:04]
Alexander Barkov
utf8_general_ci works fine. Looks like a bug in utf8_unicode_ci implementation. mysql> SELECT _utf8'X' collate utf8_general_ci LIKE '%XX%' ESCAPE 'X'; +---------------------------------------------------------+ | _utf8'X' collate utf8_general_ci LIKE '%XX%' ESCAPE 'X' | +---------------------------------------------------------+ | 1 | +---------------------------------------------------------+ 1 row in set (0.00 sec) -- Expected result mysql> SELECT _utf8'X' collate utf8_unicode_ci LIKE '%XX%' ESCAPE 'X'; +---------------------------------------------------------+ | _utf8'X' collate utf8_unicode_ci LIKE '%XX%' ESCAPE 'X' | +---------------------------------------------------------+ | 0 | +---------------------------------------------------------+ 1 row in set (0.00 sec) -- Wrong result
[13 Jul 2010 12:50]
Damien Tournoud
This is definitely still an issue: SELECT '|' COLLATE utf8_unicode_ci LIKE '|' ESCAPE '|'; => 0 (correct, the pattern is invalid) SELECT '|' COLLATE utf8_unicode_ci LIKE '||' ESCAPE '|'; => 1 (correct) SELECT '|' COLLATE utf8_unicode_ci LIKE '|||' ESCAPE '|'; => 0 (correct, the pattern is invalid) SELECT '|' COLLATE utf8_unicode_ci LIKE '||%' ESCAPE '|'; => 1 (correct) SELECT '|' COLLATE utf8_unicode_ci LIKE '%||' ESCAPE '|'; => 0 (incorrect) SELECT '|' COLLATE utf8_unicode_ci LIKE '%||%' ESCAPE '|'; => 0 (incorrect) SELECT '|' COLLATE utf8_unicode_ci LIKE '%|||%' ESCAPE '|'; => 1 (incorrect) (on MySQL 5.1.47)
[1 Oct 2020 16:43]
Ruslan Stelmachenko
Still an issue in 2020. SHOW VARIABLES LIKE 'ver%'; Variable_name |Value -----------------------|---------------------------- version |5.7.28 version_comment |MySQL Community Server (GPL) version_compile_machine|x86_64 version_compile_os |Linux SHOW VARIABLES LIKE 'char%'; Variable_name |Value ------------------------|-------------------------- character_set_client |utf8mb4 character_set_connection|utf8mb4 character_set_database |utf8mb4 character_set_filesystem|binary character_set_results | character_set_server |utf8mb4 character_set_system |utf8 character_sets_dir |/usr/share/mysql/charsets/ SELECT '|' COLLATE utf8mb4_unicode_520_ci LIKE '%||' ESCAPE '|'; -- should return 1, returns 0 SELECT '|' COLLATE utf8mb4_unicode_520_ci LIKE '%||%' ESCAPE '|'; -- should return 1, returns 0 SELECT '|' COLLATE utf8mb4_unicode_520_ci LIKE '%|||%' ESCAPE '|'; -- should return 0, returns 1
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.057 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.