這篇文章主要介紹了pg中replace和translate的用法說明(數(shù)據少的中文排序),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。
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ù)據
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('1', '大貓', '女', '一年級', '繪畫', '第三小學', '2345');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('2', '小厭', '男', '三年級', '書法', '第四小學', '2346');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('3', '庫庫', '女', '二年級', '繪畫', '第三小學', '2342');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('4', '艾琳', '女', '四年級', '書法,鋼琴', '第四小學', '2349');
結果:
1select * from students
如下圖:
3.replace 的用法
1replace(string text, from text, to text)
返回類型:text
解釋:把字串string里出現(xiàn)地所有子字串from替換成子字串to
示例1:
1select replace('一條黑色的狗','黑','黑白相間')
結果:一條黑色的狗 變成了 一條黑白相間色的狗
如下圖:
示例2:
1update students set name=replace(name,'大貓','小貓咪的姐姐')
結果:name為 ‘大貓'的這條數(shù)據name='小貓咪的姐姐'
示例3:
1select * from students where school='第四小學' ORDER BY replace(name,'艾琳','1')
結果:
4.translate的用法
1translate(string text, from text, to text)
返回類型:text
解釋:把在string中包含的任何匹配from中的字符的字符轉化為對應的在to中的字符。
示例1:
1select translate('她真是好看', '好看','漂亮')
結果:
示例2:
select * from students where phone like '2%'
ORDER BY translate(class, '一二三四','1234')
結果:
示例3:
select * from students where phone like '2%'
ORDER BY translate(name, '庫小厭貓咪艾','1234')
結果
結論:
有了translate再也不擔心中文排序問題了(數(shù)據比較少的情況)
補充: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','.',''))
文章來源:腳本之家
來源地址:https://www.jb51.net/article/204862.htm
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!