MySQL's INSTR and collations
EDIT 3: OK, forget all the complicated stuff below. My question is as easy as this: Why is the second column of the following result set 0 while all other columns are 1.
SELECT 'a' = 'á',
INSTR('András','Andras'),
'András' LIKE 'Andras',
INSTR('András','Andräs')
The database and connection is set to utf8.
END EDIT
I've got an issue with MySQL's INSTR function. I have a table 'values' with collation utf8_general_ci and a VARCHAR column 'value' containing the value 'András Schiff'. Now I perform the following queries:
> SET NAMES 'utf8' COLLATE 'utf8_general_ci'
> SELECT 'a' = 'á';
1
&g开发者_Python百科t; SELECT * FROM values WHERE value LIKE '%Andras%'
'András'
> SELECT * FROM values WHERE INSTR(value,'Andras')
(Empty)
> SELECT * FROM values WHERE INSTR(value,'Andräs')
'András'
Can anyone explain this strange behavior? I thought LIKE '%...%' and INSTR were equivalent, the latter having the advantage that the search string may contain '%'.
Thanks
EDIT: My MySQL version is Ver 14.14 Distrib 5.1.54, for debian-linux-gnu (x86_64) using readline 6.2
EDIT 2: Another thing I noticed:
> SELECT * FROM values WHERE INSTR(value,'Andras') COLLATE 'utf8_unicode_ci'
gives an error "COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'binary'". But I don't understand why the character set should be binary.
Addressing the Edit 2 portion concerning INSTR and COLLATE
The following syntax works for me
SELECT * FROM values WHERE INSTR(value COLLATE utf8_unicode_ci,'Andras')
Run this command for a database mydb
SHOW CREATE DATABASE mydb;
Example Output:
mysql> show create database conversationminer;
+-------------------+------------------------------------------------------------------------------+
| Database | Create Database |
+-------------------+------------------------------------------------------------------------------+
| conversationminer | CREATE DATABASE `conversationminer` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+-------------------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Just take note of the character set of the database.
Also, do this
SHOW CREATE TABLE values\G
Just take note of the character set of the table.
I assume that it is a bug in Mysql.
I have mysql version "mysql Ver 14.14 Distrib 5.1.67, for redhat-linux-gnu (x86_64) using readline 5.1" and it errors "COLLATION 'utf8_swedish_ci' is not valid for CHARACTER SET 'binary'".
In other server using "mysql Ver 14.14 Distrib 5.5.27, for Linux (i686) using readline 5.1" and no errors although identical databases in both.
So updating mysql could solve the problem.
精彩评论