原始问题:
Table structure:
CREATE TABLE `texts` ( `letter` VARCHAR(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `text` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, INDEX (`letter` ASC), INDEX (`text` ASC) ) ENGINE InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
Sample data:
INSERT INTO `texts` (`letter`, `text`) VALUES ('a', 'Apple'), ('ā', 'Ābols'), ('b', 'Bull'), ('c', 'Cell'), ('č', 'Čakste');
The query which I'm executing:
SELECT DISTINCT `letter` FROM `texts`;
Expected results:
`letter` a ā b c č
Actual results:
`letter` a b c
I've tried many utf8 collations (utf8_[bin|general_ci|unicode_ci], utf8mb4_[bin|general_ci|unicode_ci] etc), none of them work. How to fix this?
Edit for clarification: what I want is not just to get all the letters out, but also get them in the order I specified in the expected results.
utf8_bin
gets all the letters, but they are ordered in the wrong way - extended latin characters follow only after all the basic latin characters (example: a, b, c, ā, č). Also, the actual table I'm using has many texts per letter, so grouping is a must.
编辑 #2:这是来自实时站点的完整表格数据 - http://pastebin.com/cH2DUzf3 执行该 SQL 并在之后运行以下查询:
SELECT DISTINCT BINARY `letter` FROM `texts` ORDER BY `letter` ASC
产生几乎完美的结果,有一个异常(exception):字母“ū”在“u”之前,至少可以说这很奇怪,因为所有其他扩展拉丁语字母出现在他们的基本拉丁版本之后。我该如何解决最后一个问题?
最佳答案
关于mysql选择不同的字母,包括扩展的拉丁字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466208/