菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
143
0

PostgreSQL COALESCE使用方法代码解析

原创
05/13 14:22
阅读数 467748

常规方法:

UPDATE
  tbl_kintai_print_his
SET
  print_time = now(),
  print_emp_cd = '00编程客栈0000',
  times = (select times from tbl_kintai_print_his where  kokyaku_cd  = '000002' AND
  sagyo_ymd  = '2015-01-30' )
+ 1,
  pattern = '055' ,
  ko_itemruwvgxC_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_http://www.cppcns.com3 = '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的数值,上面的语句得到

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: PostgreSQL COALESCE使用方法代码解析
本文地址: http://www.cppcns.com/shujuku/postgresql/369291.html

发表评论

0/200
143 点赞
0 评论
收藏
为你推荐 换一批