mysql选择不同的字母,包括扩展的拉丁字符

标签 mysql sql

原始问题:

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”之前,至少可以说这很奇怪,因为所有其他扩展拉丁语字母出现在他们的基本拉丁版本之后。我该如何解决最后一个问题?

最佳答案

检查 Manual对于 BINARY 类型

SELECT DISTINCT BINARY `letter` FROM `texts` 

检查 SQL Fiddle

关于mysql选择不同的字母,包括扩展的拉丁字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466208/

相关文章:

mysql - 如何检索连续几天发生某些事件的不同行的计数

php - MySQL,根据列值从不同的行中选择

java - 无法执行查询

java - 尝试执行 PreparedStatement 时 "?"附近的 MySQLSyntaxErrorException

MySQL - str_to_date() 在更新查询中不起作用

php - 遍历数组作为php中的树

sql - 通过运行批处理文件来执行 SQL 文件

mysql按天分组

sql - 如何在 SQL Server 2008 中使用 .db 文件?

java - AliasToEntityMapResultTransformer.INSTANCE 不与 join 一起使用