Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Chinese sorting in MySQL

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to achieve Chinese sort in MySQL, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.

I didn't pay attention to this Chinese sorting problem before, so I'll record it here.

A student list

1select *from t_student;

We sort by s_name

1select *from t_student order by s_name;

The Chinese sorting here is incorrect. It should be due to the problem of character set. Generally, the coding in the database uses UTF-8, so there will be problems for Chinese.

Find 2 solutions online

Add binary attribute to create table (tested, not easy to use)

Notice the s_name field, we added the binary attribute

CREATE TABLE `t_student_test` (

`s_id` int(11) DEFAULT NULL COMMENT 'Student ID',

`s_name` varchar(20) binary DEFAULT NULL COMMENT 'student name',

`s_gender` int(11) DEFAULT NULL COMMENT 'Student gender 0-male, 1-female',

`s_birthday` date DEFAULT NULL COMMENT 'date of birth',

`s_hobby` varchar(100) DEFAULT NULL COMMENT 'Hobby',

`c_id` int(11) DEFAULT NULL COMMENT 'Class ID'

ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Student Table';

here, my experiment fail, and that Chinese ordering is still incorrect

1select *from t_student order by convert(s_name using gbk);

The above content is how to achieve Chinese sorting in MySQL. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report