MySQL select with different collation

非 Y 不嫁゛ 提交于 2020-01-05 06:45:12

问题


MySQL5.5

As I understand, collations are sets of rules to sort and compare strings, they allow for example to consider 'Mane' the same as 'Manè'. To me a collation makes sense only in the context of a comparison of two text strings.

I have a table with latin1 charset and latin1_swedish_ci collation

CREATE TABLE `swedish` (
  `c` char(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

engine, charset and collation are given constraints.

insert into swedish values ('Bar'), ('Bär'), ('Ber'), ('Bêr'), ('Yac'), ('Yaç');

I need to do a case- and accent-insensitive search with ASCII search values, I would like to use the collation latin1_german1_ci which treats those 3 couples as the same.

I've seen in the docs how to do this

-- it works with 'ber' or 'yac' as well
-- 'like' instead of '=' works as well
select c from swedish where c = _latin1 'bar' collate latin1_german1_ci;

gives

+------+
| c    |
+------+
| Bar  |
| Bär  |
+------+

OK, works, that's all I need. but why?

IIRC _latin1 'bar' collate latin1_german1_ci is a character set introducer which defines (forces?) the charset and collation of the string 'bar'. So I see two collates, swedish for the values of the column `c` and german1 for the search term.

What happens in the where clause? Has the selected string from column `c` collation latin1_swedish_ci? but then the comparison should give an error. Is it somehow converted to the collation latin1_german1_ci?

If I don't use the charset introducer the search with 'bar' only gives 'bar', which (I think) is the expected behavior of the swedish collation.

Further on, if I do

select c from swedish where c = _latin1 'bär' collate latin1_german1_ci;

It gives 0 rows, why is that?

P.S. I could not find any detailed documentation about the different collations, does anybody know where to find some info?

来源:https://stackoverflow.com/questions/41980698/mysql-select-with-different-collation

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