pg中replace和translate的用法說明(數(shù)據(jù)少的中文排序)
1.首先創(chuàng)建students表
CREATE TABLE students ( id integer NOT NULL, name character varying(255), sex character varying(255), class character varying(255), "like" character varying(255), school character varying(255), phone character varying(255) )
2.插入數(shù)據(jù)
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('1', '大貓', '女', '一年級', '繪畫', '第三小學(xué)', '2345');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('2', '小厭', '男', '三年級', '書法', '第四小學(xué)', '2346');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('3', '庫庫', '女', '二年級', '繪畫', '第三小學(xué)', '2342');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('4', '艾琳', '女', '四年級', '書法,鋼琴', '第四小學(xué)', '2349');
結(jié)果:
select * from students
如下圖:

3.replace 的用法
replace(string text, from text, to text)
返回類型:text
解釋:把字串string里出現(xiàn)地所有子字串from替換成子字串to
示例1:
select replace('一條黑色的狗','黑','黑白相間')
結(jié)果:一條黑色的狗 變成了 一條黑白相間色的狗
如下圖:

示例2:
update students set name=replace(name,'大貓','小貓咪的姐姐')
結(jié)果:name為 ‘大貓'的這條數(shù)據(jù)name='小貓咪的姐姐'

示例3:
select * from students where school='第四小學(xué)' ORDER BY replace(name,'艾琳','1')
結(jié)果:

4.translate的用法
translate(string text, from text, to text)
返回類型:text
解釋:把在string中包含的任何匹配from中的字符的字符轉(zhuǎn)化為對應(yīng)的在to中的字符。
示例1:
select translate('她真是好看', '好看','漂亮')
結(jié)果:

示例2:
select * from students where phone like '2%' ORDER BY translate(class, '一二三四','1234')
結(jié)果:

示例3:
select * from students where phone like '2%' ORDER BY translate(name, '庫小厭貓咪艾','1234')
結(jié)果

結(jié)論:
有了translate再也不擔(dān)心中文排序問題了(數(shù)據(jù)比較少的情況)
補充:pg中position、split_part、translate、strpos、length函數(shù)
我就廢話不多說了,大家還是直接看代碼吧~
select position('.' in '1.1.2.10');
select split_part('1.1.2.10','.',length('1.1.2.10') - length(translate('1.1.2.10','.',''))+1);
select split_part('1.1.2','.',length('1.1.2') - length(translate('1.1.2','.',''))+1);
select length(translate('1.1.2.10','.','a'))+1 as num
select translate('1.1.2.10','.','')
select strpos('1.1.2.10','.')
select instr('1.1.2.10','.',1,3)
select length('1.1.2.10') - length(translate('1.1.2.10','.',''))
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令小結(jié)
這篇文章主要給大家介紹了PostgreSQL數(shù)據(jù)庫中修改表字段的常用命令操作,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-12-12
PostgreSQL+Pgpool實現(xiàn)HA主備切換的操作
這篇文章主要介紹了PostgreSQL+Pgpool實現(xiàn)HA主備切換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
postgresql兼容MySQL on update current_timestamp
這篇文章主要介紹了postgresql兼容MySQL on update current_timestamp問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Postgresql 跨庫同步表及postgres_fdw的用法說明
這篇文章主要介紹了Postgresql 跨庫同步表及postgres_fdw的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

