域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要介紹了pg中replace和translate的用法說(shuō)明(數(shù)據(jù)少的中文排序),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧。
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', '大貓', '女', '一年級(jí)', '繪畫(huà)', '第三小學(xué)', '2345');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('2', '小厭', '男', '三年級(jí)', '書(shū)法', '第四小學(xué)', '2346');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('3', '庫(kù)庫(kù)', '女', '二年級(jí)', '繪畫(huà)', '第三小學(xué)', '2342');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone")
VALUES ('4', '艾琳', '女', '四年級(jí)', '書(shū)法,鋼琴', '第四小學(xué)', '2349');
結(jié)果:
1select * from students
如下圖:
3.replace 的用法
1replace(string text, from text, to text)
返回類(lèi)型:text
解釋?zhuān)喊炎执畇tring里出現(xiàn)地所有子字串from替換成子字串to
示例1:
1select replace('一條黑色的狗','黑','黑白相間')
結(jié)果:一條黑色的狗 變成了 一條黑白相間色的狗
如下圖:
示例2:
1update students set name=replace(name,'大貓','小貓咪的姐姐')
結(jié)果:name為 ‘大貓'的這條數(shù)據(jù)name='小貓咪的姐姐'
示例3:
1select * from students where school='第四小學(xué)' ORDER BY replace(name,'艾琳','1')
結(jié)果:
4.translate的用法
1translate(string text, from text, to text)
返回類(lèi)型:text
解釋?zhuān)喊言趕tring中包含的任何匹配from中的字符的字符轉(zhuǎn)化為對(duì)應(yīng)的在to中的字符。
示例1:
1select 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, '庫(kù)小厭貓咪艾','1234')
結(jié)果
結(jié)論:
有了translate再也不擔(dān)心中文排序問(wèn)題了(數(shù)據(jù)比較少的情況)
補(bǔ)充:pg中position、split_part、translate、strpos、length函數(shù)
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~
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','.',''))
文章來(lái)源:腳本之家
來(lái)源地址:https://www.jb51.net/article/204862.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!