這篇文章主要介紹了postgresql 實(shí)現(xiàn)字符串分割字段轉(zhuǎn)列表查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧。
在數(shù)據(jù)查詢中,有一張a表存有另一張b表的id并以‘,'隔開
如:
假設(shè)現(xiàn)在要關(guān)聯(lián)查詢關(guān)于 b表的一些信息,怎么辦。
分割查詢:字符串轉(zhuǎn)列表函數(shù) :regexp_split_to_table()
1select * from regexp_split_to_table ((select product_ids from fee_project_meal where id = 116199376233182210 ), ',')
查詢后,字符串就變成了列表,然后你就可以根據(jù)這個(gè)列表去找b表的相關(guān)信息了。
select *
from pm.product
where id::text in
(select * from regexp_split_to_table ((select product_ids from bp.fee_project_meal where id = 116199376233182210 ), ','))
首先數(shù)據(jù)驗(yàn)證是正確的,說明sql沒有問題,接下來就是一起關(guān)聯(lián)查詢了
1.因?yàn)檫@個(gè)a表與b表是一對(duì)多的關(guān)系,所以我們先關(guān)聯(lián)出多條。
select a.id as "a表_id",
a.name as "a表_name",
p.name as "b表_name"
from bp.fee_project_meal a
LEFT JOIN pm.product p on p.id::text
in (select * from regexp_split_to_table ((select product_ids from bp.fee_project_meal where id = a.id ), ','))
where a.id = 116199376233182210
2.還有一種就是 我只要查出a表的數(shù)據(jù),b表的數(shù)據(jù)中某些字段做未拼接的形式存在,也就是說 現(xiàn)在要查出a表的數(shù)據(jù)
SELECT
a.id as "a表_id",
a.name as "a表_name",
bb.p_id as "b表_拼接id",
bb.p_name as "b表_拼接name"
from bp.fee_project_meal a
left join (
select a.id as "bb_id",String_agg(p.id::text,',') as "p_id",String_agg(p.name::text,',') as "p_name"
from bp.fee_project_meal a
LEFT JOIN pm.product p on
p.id::text in (select * from regexp_split_to_table ((select product_ids from bp.fee_project_meal where id = a.id ), ','))
GROUP BY 1
) bb on bb."bb_id" = a.id
以上就是,字符串字段的拆解查詢。
補(bǔ)充:pgsql 查詢字段中根據(jù)逗號(hào)分隔的字符串的的 個(gè)數(shù)
1select length(translate(column,','||column,','))+1 from table
參見:
1.translate 與replace類似是替換函數(shù),但translate是一次替換多個(gè)單個(gè)的字符。
2.基本用法,字符對(duì)應(yīng)替換。
例子:
1select translate('1234567','123' ,'abc') from dual ;--1替換為a,2替換為b,3替換為c
結(jié)果:abc4567 。
3.如果 沒有對(duì)應(yīng)字符則替換為null;
1select translate('1234567','123' ,'ab') from dual;--3替換為null;
結(jié)果:ab4567.
4.如果對(duì)應(yīng)字符過多,不影響
1select translate('1234567','123' ,'abccd') from dual;
結(jié)果:abc4567
5.如果替換字符整個(gè)為空字符 ,則直接返回null
1select translate('1234567','123' ,'') from dual;
結(jié)果:null;
6.如果想篩掉對(duì)應(yīng)字符,應(yīng)傳入一個(gè)不相關(guān)字符,同時(shí)替換字符也加一個(gè)相同字符;
1select translate('1234567','&123' ,'&') from dual;
結(jié)果:4567;
7.如果相同字符對(duì)應(yīng)多個(gè)字符,按第一個(gè);
1select translate('12334567','1233' ,‘abcd') from dual;
結(jié)果:abcc4567;
8.如果想保留某些特定字符篩選掉其他的,比如篩掉漢字保留數(shù)字
先把數(shù)字篩選掉,
1select translate('你師看了3三樓2的6開8發(fā)','#0123456789' ,'#') from dual
再用篩選出的漢字去篩選原來的語句留下數(shù)字,
1select translate('你師看了3三樓2的6開8發(fā)','#'||translate('你師看了3三樓2的6開8發(fā)','#0123456789' ,'#'),'#') from dual;
結(jié)果:3268;
9.還有其他靈活用法,比如我可以判斷兩個(gè)字符串如果:字符串都是數(shù)字字符,然后數(shù)字字符的順序不同,且每個(gè)字符只出現(xiàn)一次,
我可以判斷他們包含的數(shù)字是不是完全一致;
比如比較123 和132;
1select 1 from dual where
translate('0123456789','123' ,'aaaaaaaaaa') =translate('0123456789','132' ,'aaaaaaaaaa')
文章來源:腳本之家
來源地址:https://www.jb51.net/article/205156.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!