PostgreSQL COALESCE使用方法代码解析

作者:手撕高达的村长 时间:2024-01-28 19:55:01 

有这种要求,更新自己本身的字段的某个值进行加或者减

常规方法:


UPDATE
 tbl_kintai_print_his
SET
 print_time = now(),
 print_emp_cd = '000000',
 times = (select times from tbl_kintai_print_his where  kokyaku_cd  = '000002' AND
 sagyo_ymd  = '2015-01-30' )
+ 1,
 pattern = '055' ,
 ko_item_1 = 'no.0' ,
 ko_item_2 = 'no.2' ,
 ko_item_3 = 'no.3' ,
 ko_item_4 = 'no.4' ,
 ko_item_5 = 'no.5'
WHERE
 kokyaku_cd  = '000002' AND
 sagyo_ymd  = '2015-01-30'

能实现,不过效率肯定不高,要进行查询两次

pgsql里面提供一个函数能完成这个操作:


UPDATE
 tbl_kintai_print_his
SET
 print_time = now(),
 print_emp_cd = '000000',
 times = COALESCE (SUM(times),0)+ 1,
 pattern = '055' ,
 ko_item_1 = 'no.0' ,
 ko_item_2 = 'no.2' ,
 ko_item_3 = 'no.3' ,
 ko_item_4 = 'no.4' ,
 ko_item_5 = 'no.5'
WHERE
 kokyaku_cd  = '000002' AND
 sagyo_ymd  = '2015-01-30'

能直接取到上一次的结果进行添加

二:还有一种用法就是在几个字段中取值,从前往后,一直取到不为NULL的值为止。

select id , name ,coalesce(Ph_no,Alt_no,Office_no) as contact number from employee

我们可以通过这样的语句,来得到Ph_no,Alt_no,Office_no这几个字段中,第一个不存在null的数值,上面的语句得到

来源:https://www.cnblogs.com/sunxun/p/4260023.html

标签:PostgreSQL,COALESCE
0
投稿

猜你喜欢

  • php函数之strtr和str_replace的用法详解以及效率分析

    2023-06-02 14:04:18
  • 如何在ACCESS中插入超级链接?

    2007-10-22 12:13:00
  • Microsoft VBScript 运行时错误 错误 800a0005 无效的过程调用或参数: chr

    2011-03-09 11:03:00
  • Python中django学习心得

    2023-12-24 17:10:15
  • pandas DataFrame的修改方法(值、列、索引)

    2021-10-17 11:35:10
  • 如何对PHP程序中的常见漏洞进行攻击(下)

    2023-11-16 14:50:19
  • python求质数列表的例子

    2021-01-10 01:42:56
  • AI与Python人工智能启发式搜索概念理解

    2021-08-05 03:56:28
  • MYSQL 数据库同步

    2008-11-24 12:39:00
  • python 判断参数为Nonetype类型或空的实例

    2021-03-21 07:15:17
  • Pytorch 数据加载与数据预处理方式

    2021-06-12 11:07:05
  • Python3爬虫关于识别检验滑动验证码的实例

    2021-08-25 02:17:37
  • 详解vue3.0 的 Composition API 的一种使用方法

    2024-04-28 09:23:48
  • python远程登录代码

    2022-09-12 15:48:15
  • 网页设计的色彩思考

    2007-10-19 13:30:00
  • 教你使用Python 的 Template 类生成文件报告

    2022-01-27 04:52:24
  • Python 如何实现文件自动去重

    2021-07-16 13:50:54
  • 在Django中进行用户注册和邮箱验证的方法

    2022-02-01 16:18:35
  • python 实现简单的计算器(gui界面)

    2022-11-14 14:35:14
  • vue使用微信JS-SDK实现分享功能

    2023-07-02 16:59:46
  • asp之家 网络编程 m.aspxhome.com