Table of Contents
回复内容:
Home Backend Development PHP Tutorial mysql查询结果排序,name字段有中文名字,有英文名字,如何按a-z的顺序把名字排序

mysql查询结果排序,name字段有中文名字,有英文名字,如何按a-z的顺序把名字排序

Jun 06, 2016 pm 08:09 PM
java mysql php yii

1、name字段用来存储客户姓名,查询结果需要按姓氏拼音排序,现在有一个问题,
名字既有中文名字,又有英文名字,比如 张三,李四,Jaewon Park,老王
这样查询出来的结果英文名字的人永远排第一
排序我使用的是下面这句
CONVERT( name USING gbk ) COLLATE gbk_chinese_ci
mysql查询结果排序,name字段有中文名字,有英文名字,如何按a-z的顺序把名字排序

如何可以将英文的第一个字母也按照拼音的方式排序呢?

回复内容:

1、name字段用来存储客户姓名,查询结果需要按姓氏拼音排序,现在有一个问题,
名字既有中文名字,又有英文名字,比如 张三,李四,Jaewon Park,老王
这样查询出来的结果英文名字的人永远排第一
排序我使用的是下面这句
CONVERT( name USING gbk ) COLLATE gbk_chinese_ci
mysql查询结果排序,name字段有中文名字,有英文名字,如何按a-z的顺序把名字排序

如何可以将英文的第一个字母也按照拼音的方式排序呢?

谢邀。
单纯用 mysql 解决,中英文混合还要按照 A-Z 排序,我并没有办法

我的一个小思路是,可以增加一列,把中文转成拼音,然后按照字母排序

新建一列,用来保存 如果是英文 就保存首字母,如果是中文就保存拼音首字母,专门用来排序
至少汉字拼音怎么获得,网上解决方法很多,自己搜一下吧

多加一列,保存对应名字的拼音,这样就很好排序了

`--创建获取汉字首字母的函数
CREATE FUNCTION FirstPinyin(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN

<code>DECLARE V_RETURN VARCHAR(255);
SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAME USING gbk),1)),16,10), 
    0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7, 
    0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
    0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),    
'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
    RETURN V_RETURN;</code>
Copy after login

END
--编写查询语句
SELECT * FROM article ORDER BY IFNULL(FirstPinyin(title),substring(title, 1, 1)) ASC`

将中文转拼音,然后在根据首字母排序

推荐中文转拼音来处理 基于词库的中文转拼音优质解决方案

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Using Laravel: Streamlining Web Development with PHP Using Laravel: Streamlining Web Development with PHP Apr 19, 2025 am 12:18 AM

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

What software is better for yi framework? Recommended software for yi framework What software is better for yi framework? Recommended software for yi framework Apr 18, 2025 pm 11:03 PM

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

See all articles