MySQL中的COLLATE关键字

Simply put

Collation in MySQL refers to the set of rules used to compare and sort characters in a particular character set. It determines how strings are compared and ordered based on their characters’ linguistic and cultural rules. Collation settings affect operations such as sorting, searching, and comparing strings in MySQL queries.

In MySQL, collation can be specified at different levels:

  1. Database Level: Collation can be set at the database level during database creation or by altering the database collation.
  2. Table Level: Collation can be set at the table level during table creation or by altering the table collation.
  3. Column Level: Collation can be set at the column level during column definition or by altering the column collation.

MySQL provides a variety of collations, each designed for specific character sets and languages. The collation name typically includes the character set name and a suffix indicating the collation rules. For example, “utf8_general_ci” is a collation for the UTF-8 character set using the case-insensitive comparison rule.

Collation rules determine how characters are compared, considering factors such as case sensitivity, accent sensitivity, and character weight. Some collations are case-insensitive, meaning they treat uppercase and lowercase characters as the same. Others are case-sensitive and distinguish between uppercase and lowercase characters. Similarly, some collations are accent-insensitive, treating accented and unaccented characters as equal, while others are accent-sensitive.

By specifying the appropriate collation for your data, you can ensure that string comparisons and sorting operations in MySQL adhere to the desired linguistic and cultural rules.

说明

MySQL中的COLLATE关键字用于指定字符集的排序规则(collation)。排序规则指定了字符应该如何排序和比较。默认情况下,MySQL使用字符集指定的排序规则进行字符串比较。但是,您可以使用COLLATE关键字来覆盖默认排序规则,并为特定操作指定不同的排序规则。例如,您可以使用COLLATE关键字来对表中的列进行不同于表指定的排序规则的排序。

使用示例

SELECT

SELECT column_name 
FROM table_name 
ORDER BY column_name COLLATE utf8_general_ci; 

In this example, the COLLATE keyword is used to specify the collation “utf8_general_ci” for the column “column_name” in the ORDER BY clause. This collation determines the comparison and sorting rules for the column’s character data.

Table

CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(50) COLLATE utf8_general_ci
);

INSERT INTO students (id, name) VALUES (1,'Alice'), (2,'bob'), (3,'Charlie'), (4,'delia');

If you want to retrieve the names of the students in alphabetical order using a case-sensitive sorting rule, you can use the COLLATE keyword as follows:

SELECT name FROM students ORDER BY name COLLATE utf8_bin;

The above query will return the names in the order: Alice, Charlie, bob, delia.

Note that if you had not used the COLLATE keyword, the names would have been sorted using the default collation rule for the column (“utf8_general_ci”), which is case-insensitive, and the order would have been: Alice, bob, Charlie, delia.

It may be that GPT knows more ^^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值