SQL编写技巧

1.left join 后面加 判断语句 和 where 加 判断语句有什么区别

left join 后加where 约束整个查询结果;

left join 后加and,and条件不能限制左表,仅能限制右表。

例如 from a left join b on a.id=b.id and a.id<5; 此and后条件无效;

例如 form a left join b on a.id=b.id and b.id<5; 可以限制查询结果中b的id小于5;

例如 from a left join b on a.id=b.id where a.id<5; 此where条件可以成功约束查询结果中a的id小于5;

2.为查询出来的语句添加行号

    ROW_NUMBER()OVER(ORDER BY ID DESC) AS rank,

3.pgsql 字符串转json数组

①将整张表 转为json数组

array_to_json( array_agg (row_to_json(表名))) AS JsonArray

②将某个字段转为 json数组

array_to_json ( array_agg(( 字段名))) AS JsonArray

③将字段分组后合并

string_agg

4.pgsql 分割

split_part(字段名,'根据什么分割',下标)

例子:split_part ( plan.businessunit, '-', 1 ) AS area

5.pgsql 替换

replace(字段名,'要替换得值','要替换成得值')

replace(string_agg ( file_template, ',' ),'],[',',') 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值