PostgresSQL高级应用

1.替换

UPDATE 表名 SET 字段名=REPLACE(a,b,c)   -- 将字段(a)里的字符(b)批量替换为字符(c)

2.截取–返回从字符串左边开始指定个数的字符

(1)从左边截取LEFT(character,integer) 参数1:要截取的字符串,参数2:截取字符个数

update demo set publish_time=LEFT(publish_time,10);

(2)从右边截取

update demo set publish_time=RIGHT(publish_time,10);

(3)从中间截取SUBSTRING(character,start,length)

update demo set publish_time=SUBSTRING(publish_time,10,20);

3.正则替换

Update "表名" set 字段=REGEXP_REPLACE(keyword, '^(doc;)|^(pdf;)|(;doc&)|(;pdf&)|^(nbsp;)|(;nbsp$)', '', 'g');
update "BasicData" set keyword=REGEXP_REPLACE(keyword, ';[\d.]*;', ';', 'g');
update "BasicData" set keyword=REGEXP_REPLACE(keyword, '(^[A-Za-z0-9.%_]+)+;', '', 'g');
update "BasicData" set keyword=REGEXP_REPLACE(keyword, '(;[A-Za-z0-9.%_]+)+$', '', 'g');
update "BasicData" set keyword=REGEXP_REPLACE(keyword, '(;[A-Za-z0-9.%_]+)+;', '', 'g');

4.分组合并多行数据(字段必须是字符串类型)

select id,string_agg(字段,'分隔符') from "fff" GROUP BY id;
select class_type, array_to_string(array_agg(name),'-') as names from user_account group by class_type;

5.拼接

update "ttt" set con=concat("ttt".con ,';',"fff".con) from "fff" where "ttt".file_name="fff".file_name and  "fff".id="ttt".id  and "ttt".id= 25;

6.整行去重

select * from fff union select * from fff;

7.字符串转数字

update "PartyRules" set release_year=cast(left(implement_date,4) as int) where release_year is null;

8.字符串转日期

update tables_basicdata set publish_date=to_date(release_date,'yyyy-MM-dd') where 

9.日期转数字

update rules set years= cast(left(to_char(publish_date,'YYYY-MM-DD'),4) as int)

10.匹配返回多行

regexp_matches(string, pattern[, flags ])
SELECT id,regexp_matches(con, '(《[^》]+《)','g') as f FROM "ttt" ORDER BY id;

11.差集、并集、交集

(1) Union 获取两个表的合集并且自动过滤重复数据

Select * from a
Union
Select * from b

(2)Union all 获取两个表的合集并且不过滤重复数据

Select * from a
Union all
Select * from b

(3)Intersect 获取两个表的交集

Select * from a
Intersect
Select * from b

(4)except 获取第1个表中存在,而第2个表中不存在的数据

Select * from a
Except
Select * from b
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值