PostgreSQL 按拼音排序 - convert to GBK/EUC_CN coding
背景
国内的应用,在文本排序上基本都是按照拼音来进行排序的。
在不同的字符集中,汉字的编码可能不一样,比如UTF8和GBK,其中GBK是按拼音的顺序进行编码的,而UTF8则不是。
所以如果你的数据库使用了UTF8编码,对中文字段进行排序时,可能得到的并不是按拼音排序的结果。
在PostgreSQL中,中文按拼音排序的编码包括GB18030, EUC_CN, GBK, BIG5, EUC_TW 等。
为了得到拼音排序,可以使用编码转换后的值来排序,索引也可以使用编码转换的表达式索引。
编码
PostgreSQL支持的编码如下
https://www.postgresql.org/docs/9.6/static/multibyte.html
与中文编码排序相关的包括 GB18030, EUC_CN, GBK, BIG5, EUC_TW
简体常用的包括GBK, EUC_CN。
编码转换
在PostgreSQL中,如果要将字符从一个编码转换为另一个编码,需要告诉数据库(create conversion)怎么转换(使用什么C函数),PG内置了一些转换的C函数和转换方法。
https://www.postgresql.org/docs/9.6/static/catalog-pg-conversion.html
pg_conversion
| Name | Type | References | Description |
|---|---|---|---|
| oid | oid | - | Row identifier (hidden attribute; must be explicitly selected) |
| conname | name | - | Conversion name (unique within a namespace) |
| connamespace | oid | pg_namespace.oid | The OID of the namespace that contains this conversion |
| conowner | oid | pg_authid.oid | Owner of the conversion |
| conforencoding | int4 | - | Source encoding ID |
| contoencoding | int4 | - | Destination encoding ID |
| conproc | regproc | pg_proc.oid | Conversion procedure |
| condefault | bool | - | True if this is the default conversion |
查看内置的转换方法
可以看到utf8转换为中文编码的都支持了
postgres=> select * from pg_conversion where conname ~* 'gbk|gb18|euc_cn|euc_tw|big5' order by 1;
conname | connamespace | conowner | conforencoding | contoencoding | conproc | condefault
-----------------+--------------+----------+----------------+---------------+-----------------+------------
big5_to_euc_tw | 11 | 10 | 36 | 4 | big5_to_euc_tw | t
big5_to_mic | 11 | 10 | 36 | 7 | big5_to_mic | t
big5_to_utf8 | 11 | 10 | 36 | 6 | big5_to_utf8 | t
euc_cn_to_mic | 11 | 10 | 2 | 7 | euc_cn_to_mic | t
euc_cn_to_utf8 | 11 | 10 | 2 | 6 | euc_cn_to_utf8 | t
euc_tw_to_big5 | 11 | 10 | 4 | 36 | euc_tw_to_big5 | t
euc_tw_to_mic | 11 | 10 | 4 | 7 | euc_tw_to_mic | t
euc_tw_to_utf8 | 11 | 10 | 4 | 6 | euc_tw_to_utf8 | t
gb18030_to_utf8 | 11 | 10 | 39 | 6 | gb18030_to_utf8 | t
gbk_to_utf8 | 11 | 10 | 37 | 6 | gbk_to_utf8 | t
mic_to_big5 | 11 | 10 | 7 | 36 | mic_to_big5 | t
mic_to_euc_cn | 11 | 10 | 7 | 2 | mic_to_euc_cn | t
mic_to_euc_tw | 11 | 10 | 7 | 4 | mic_to_euc_tw | t
utf8_to_big5 | 11 | 10 | 6 | 36 | utf8_to_big5 | t
utf8_to_euc_cn | 11 | 10 | 6 | 2 | utf8_to_euc_cn | t
utf8_to_euc_tw | 11 | 10 | 6 | 4 | utf8_to_euc_tw | t
utf8_to_gb18030 | 11 | 10 | 6 | 39 | utf8_to_gb18030 | t
utf8_to_gbk | 11 | 10 | 6 | 37 | utf8_to_gbk | t
(18 rows)
编码转换函数
注意数据库版本
PostgreSQL 8.x(如Greenplum), 将字符串从原编码转换为指定编码的字符串返回。
可能存在显示的问题。
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------------+------------------+---------------------+--------
pg_catalog | convert | text | text, name | normal
pg_catalog | convert | text | text, name, name | normal
PostgreSQL 9.x, 将源编码字符串的字节流转换为指定编码的字符串的字节流返回。
避免了显示的问题。
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+--------------+------------------+---------------------+--------
pg_catalog | convert | bytea | bytea, name, name | normal
pg_catalog | convert_from | text | bytea, name | normal
pg_catalog | convert_to | bytea | text, name | normal
如果8.x需要避免显示问题,返回字节流,可以这样使用,推荐使用。
byteain(textout(convert(字符,'源编码','目标编码')))
例子
当前数据库编码为UTF-8,中文排序未按拼音排序。
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
db0 | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# select * from (values ('*德华'), ('***')) t(id) order by id;
id
--------
***
*德华
(2 rows)
按拼音排序方法(目标可以改成EUC_CN)
8.x
postgres=> select * from (values ('*德华'), ('***')) t(id) order by byteain(textout(convert(id,'UTF-8','GBK')));
id
--------
*德华
***
(2 rows)
9.x
postgres=# select * from (values ('*德华'), ('***')) t(id) order by convert(id::bytea,'UTF-8','GBK');
id
--------
*德华
***
(2 rows)
注意多音字
中文有一些多音字,比如重庆(chongqing), 但是编码时它可能是按zhong编码的,所以看这个例子。
postgres=> select * from (values ('中山'), ('重庆')) t(id) order by byteain(textout(convert(id,'UTF-8','GBK')));
id
------
中山
重庆
(2 rows)
索引
表达式索引即可,使用immutable function.
代码
8.x
postgres=> \df+ convert
List of functions
Schema | Name | Result data type | Argument data types | Type | Data access | Volatility | Owner | Language | Source code | Description
------------+---------+------------------+---------------------+--------+-------------+------------+-----------+----------+-------------+---------------------------------------------------------
pg_catalog | convert | text | text, name | normal | no sql | stable | xxx | internal | pg_convert | convert string with specified destination encoding name
pg_catalog | convert | text | text, name, name | normal | no sql | stable | xxx | internal | pg_convert2 | convert string with specified encoding names
(2 rows)
9.x
postgres=# \df+ convert
List of functions
Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code | Description
------------+---------+------------------+---------------------+--------+------------+----------+----------+----------+-------------------+----------+-------------+----------------------------------------------
pg_catalog | convert | bytea | bytea, name, name | normal | stable | safe | postgres | invoker | | internal | pg_convert | convert string with specified encoding names
(1 row)
src/backend/utils/mb/mbutils.c
/*
* Convert string between two arbitrary encodings.
*
* BYTEA convert(BYTEA string, NAME src_encoding_name, NAME dest_encoding_name)
*/
Datum
pg_convert(PG_FUNCTION_ARGS)
{
bytea *string = PG_GETARG_BYTEA_PP(0);
char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
int src_encoding = pg_char_to_encoding(src_encoding_name);
char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
int dest_encoding = pg_char_to_encoding(dest_encoding_name);
const char *src_str;
char *dest_str;
bytea *retval;
int len;
if (src_encoding < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid source encoding name \"%s\"",
src_encoding_name)));
if (dest_encoding < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid destination encoding name \"%s\"",
dest_encoding_name)));
/* make sure that source string is valid */
len = VARSIZE_ANY_EXHDR(string);
src_str = VARDATA_ANY(string);
pg_verify_mbstr_len(src_encoding, src_str, len, false);
/* perform conversion */
dest_str = (char *) pg_do_encoding_conversion((unsigned char *) src_str,
len,
src_encoding,
dest_encoding);
/* update len if conversion actually happened */
if (dest_str != src_str)
len = strlen(dest_str);
/*
* build bytea data type structure.
*/
retval = (bytea *) palloc(len + VARHDRSZ);
SET_VARSIZE(retval, len + VARHDRSZ);
memcpy(VARDATA(retval), dest_str, len);
if (dest_str != src_str)
pfree(dest_str);
/* free memory if allocated by the toaster */
PG_FREE_IF_COPY(string, 0);
PG_RETURN_BYTEA_P(retval);
}
使用字段或排序collate语法纠正排序顺序
使用binary存储格式排序,只能通过编码来修正排序顺序。
除此之外,我们还可以在不改编码的情况下,使用字段或者order by的collate语法来修正排序顺序。
例子
select * from pg_collation ;
设置列级collate
create table a (c1 text collate "zh_CN.utf8");
修改列collate,会导致rewrite table
alter table a alter c1 type text COLLATE "zh_CN.utf8";
设置排序级collate
test=# select * from a order by c1 collate "C";
c1
--------
***
*德华
(2 rows)
test=# select * from a order by c1 collate "zh_CN.utf8";
c1
--------
*德华
***
(2 rows)
设置operator collate
test=# select * from a where c1 > '***' collate "C";
c1
--------
*德华
(1 row)
test=# select * from a where c1 > '***' collate "zh_CN.utf8";
c1
----
(0 rows)
设置库级collate
postgres=# create database test with template template0 encoding 'UTF8' lc_collate 'zh_CN.utf8';
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table a (c1 text);
CREATE TABLE
test=# insert into a values ('*德华'),('***');
INSERT 0 2
test=# select * from a order by c1;
c1
--------
*德华
***
(2 rows)
注意索引和创建索引时的collate必须一致,才能使用该索引
postgres=# create table a(c1 text);
CREATE TABLE
postgres=# create index idxa on a(c1 collate "zh_CN.utf8");
CREATE INDEX
postgres=# explain select * from a order by c1;
QUERY PLAN
----------------------------------------------------------------
Sort (cost=10000000094.38..10000000097.78 rows=1360 width=32)
Sort Key: c1
-> Seq Scan on a (cost=0.00..23.60 rows=1360 width=32)
(3 rows)
postgres=# explain select * from a order by c1 collate "zh_CN.utf8";
QUERY PLAN
------------------------------------------------------------------------
Index Only Scan using idxa on a (cost=0.15..31.55 rows=1360 width=64)
(1 row)
PostgreSQL 按拼音排序 - convert to GBK/EUC_CN coding的更多相关文章
- PostgreSQL下,对汉字按拼音排序
参考学习此文: http://blog.163.com/digoal@126/blog/static/163877040201173003547236/ 建库 postgres=# \l List o ...
- PostgreSQL对汉字按拼音排序
转自:https://www.cnblogs.com/gaojian/p/3188609.html postgres=# \l List of databases Name | Owner | Enc ...
- MySQL按照汉字的拼音排序
按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...
- MySQL按照汉字的拼音排序,mysql汉字排序
按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...
- ecshop添加商品选择品牌时如何按拼音排序
ECSHOP后台添加新商品时,有一个选择品牌的下拉框,如果品牌太多,在下拉框里查找起来很不方便. 我想给“下拉框里的品牌列表”按品牌名的拼音排序,比如有“中国水利出版社” “中国人民出版社” 这两个品 ...
- MySQL按照汉字的拼音排序(转)
按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...
- PHP 字符串数组按照拼音排序的问题
拼音排序的规则: 字符串包括特殊字符.数字.英文字符.中文字符等等,排序结果要求,特殊字符排在第一梯队,将其按照首个字符ascii码表进行排序,数字字符排在第二梯队,将首个字符数字按照数字大小排序,英 ...
- MySQL按照汉字的拼音排序、按照首字母分类
项目中有时候需要按照汉字的拼音排序,比如联系人列表.矿物分类等,有的还需要按拼音字母从A到Z分类显示. 如果存储汉字的字段编码使用的是GBK字符集,因为GBK内码编码时本身就采用了拼音排序的方法(常用 ...
- sqlalchemy & python & datatables & javascript 中文拼音排序
近期有中文拼单排序需要,查询资料,mysql数据库有convert函数支持 select cname from channel order by convert(cname using gbk); # ...
- TP5 按照汉字的拼音排序
业务需求:接口返回一个列表,但是这个列表要求按一定的条件排序,条件如下: 1,某字段(field1)为null的排前面 2,某字段(field2)为null的排前面 3,姓名(field3)按照汉字的 ...
随机推荐
- 【每日一题】【与运算判断奇偶】【list的重载前后插入】2021年11月25日-103. 二叉树的锯齿形层序遍历
给定一个二叉树,返回其节点值的锯齿形层序遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,null,15,7], 来源:力扣 ...
- latex 中添加Springer LNCS 模板的\bibitem{}格式参考文献方法
1.将需要引用的参考文献新建为.bib格式,例如referencesTest.bib文件,具体如下: 新建txt文件,后缀名改为.bib: 然后打开谷歌学术,搜索参考文献, 点击导入BibTeX,具体 ...
- [python]《Python编程快速上手:让繁琐工作自动化》学习笔记1
1. 模式匹配与正则表达式笔记(第7章)(代码下载) 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念. ...
- JS比较数值大小
一. 简单循环算法 代码如下: const numbers = [5, 6, 2, 3, 7]; let max = -Infinity; for (let i = 0; i < numbers ...
- AspNetCore管道
title: Asp.Net Core底层源码剖析(一)中间件/管道 categories: 后端 tags: - .NET 当我们像下面这样添加一个管道时发生了什么? app.Use(async ( ...
- 数字IC设计流程
数字IC设计流程 简单介绍数字IC设计流程
- 如何找到CSDN中关注的用户和粉丝?
如何找到CSDN中关注的用户和粉丝? 刚刚在CSDN个人账号里找了半天都没找到自己关注的人 对CSDN的页面更新感到很迷, 个人账号管理很不人性化, 或者说是根本找不到自己关注的用户以及关注自己的粉丝 ...
- java跨域问题解决
问题描述:在使用前后端分离的情况下,前端访问后端时会出现跨域问题 解决方式: 1.设置跨域 1).单个控制器方法CORS注解 在单个方法中加入注解@CrossOrigin. 2).整个控制器启用COR ...
- 【随笔记】Littlevgl 8.x 踩过的坑
在多线程并发使用的时候,总是在运行过程中莫名其妙的 crash,后面才意识到 LVGL 本身不支持并发,加了互斥锁解决了. 在引入矢量字库时(freetype),又有启动时会概率性 crash 的问题 ...
- 软工综合实践课设——员工招聘系统(参考BOSS直聘);Pyhton实现
应用背景: 随着科学技术的发展,岗位数量越来越多,特别是每逢毕业季找工作的人数也很多,如果人们找工作或者企业招人靠纯手工的话,费时费力,仅仅是筛选简历和费劲,并且员工找工作投简历可能得需要克服时间和空 ...