数据库中文字段按拼音排序

使用 order by convert_to(city, ‘GBK’) 方式按中文字典顺序排序, 测试如下:

create table public.test_city(id bigserial primary key, city char(10));
test#\d jinbo.test_city;
                                Table "public.test_city"
 Column |     Type      |                          Modifiers                           
--------+---------------+--------------------------------------------------------------
 id     | bigint        | not null default nextval('public.test_city_id_seq'::regclass)
 city   | character(10) | 
Indexes:
    "test_city_pkey" PRIMARY KEY, btree (id)

test=# insert INTO public.test_city(city) values ('上海'),('北京'),('广州');
INSERT 0 3
test=# insert INTO public.test_city(city) values ('西安'),('榆林'),('三亚');
INSERT 0 3
test=# insert INTO public.test_city(city) values ('深圳'),('鞍山'),('昆明');
INSERT 0 3
test=# insert INTO public.test_city(city) values ('大连'),('青岛'),('成都');
INSERT 0 3

test=# select * from public.test_city;
 id |     city     
----+--------------
  1 | 上海        
  2 | 北京        
  3 | 广州        
  4 | 西安        
  5 | 榆林        
  6 | 三亚        
  7 | 深圳        
  8 | 鞍山        
  9 | 昆明        
 10 | 大连        
 11 | 青岛        
 12 | 成都        
(12 rows)

test=# select * from public.test_city order by convert_to(city, 'GBK');
 id |     city     
----+--------------
  8 | 鞍山        
  2 | 北京        
 12 | 成都        
 10 | 大连        
  3 | 广州        
  9 | 昆明        
 11 | 青岛        
  6 | 三亚        
  1 | 上海        
  7 | 深圳        
  4 | 西安        
  5 | 榆林        
(12 rows)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值