MySQL's INSTR and collations

岁酱吖の 提交于 2019-12-22 09:51:52

问题


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


回答1:


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.




回答2:


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.




回答3:


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')


来源:https://stackoverflow.com/questions/7393724/mysqls-instr-and-collations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!