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

常规方法:

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的数值,上面的语句得到

Pgsql 里面 COALESCE的用法的更多相关文章

  1. coalesce函数用法

    COALESCE函数会依次检查输入的参数,返回第一个不是NULL的参数,只有当传入COALESCE函数的所有的参数都是NULL的时候,函数才会返回NULL

  2. mssql sqlserver isnull coalesce函数用法区别说明

    摘要: 下文讲述isnull及coalesce空值替换函数的区别 isnull.coalesce函数区别:1.isnull 只能接受两个参数,而coalesce函数可以接受大于等于两个以上参数2.is ...

  3. MySQL coalesce函数用法说明(转)

    在mysql中,其实有不少方法和函数是很有用的,一个叫coalesce的,作用是将返回传入的参数中第一个非null的值,比如 SELECT COALESCE(NULL, NULL, 1); -- Re ...

  4. SqlServer coalesce函数

    SqlServer数据库中coalesce函数用法:在SqlServer2005中有了新的函数,它非常的实用,它就是coalesce函数,此函数可以返回参数中的第一个非空表达式,当你要在N个字段中选取 ...

  5. coalesce搭配nullif使用

    with t1 as ( select NUll as col1, '' as col2, 'aaa' as col3 ) --关于COALESCE用法 当col1 为 Null时候返回 col2 依 ...

  6. 常用Mysql或者PostGresql或者Greenplum的语句总结。

    1.使用mysql的union all可以同时查询出所有自己想要查询数据表的数据量. select 'user' as tablename, count(*) from user union all ...

  7. mssql sqlserver null数据类型专题

    摘要: 下文将详细讲述sql server NULL(空值)的相关知识,如下所示: 实验环境: sql server 2008 R2 NULL(空值)简介: mssql sqlserver null数 ...

  8. 关于 pgsql 数据库json几个函数用法的效率测试

    关于 pgsql 数据库json几个函数用法的效率测试 关于pgsql 几个操作符的效率测试比较1. json::->> 和 ->> 测试方法:单次运行100次,运行10个单次 ...

  9. Mysql coalesce()函数认识和用法

    Mysql coalesce()函数认识和用法   coalesce()解释:返回参数中的第一个非空表达式(从左向右):    鉴于在mysql中没有nvl()函数, 我们用coalesce()来代替 ...

随机推荐

  1. 关于如何在BCB中使用CodeGuard

    作者:深圳虫 来自:深圳虫网本文来自http://www.szbug.com/disparticle.aspID=4 一. 为什么写这篇东西自己在使用BCB5写一些程序时需要检查很多东西,例如内存泄漏 ...

  2. 【HDOJ】1525 Euclid's Game

    自己想明白的第一道博弈.首先a==b的时候肯定是先手赢: 然后当a>=2*b时,不妨假设a=nb+k, k<b,因此,不论后续怎么博弈,一定可以出现a=k, b=b的情况.因此,无论这个局 ...

  3. 【HDOJ】2487 Ugly Windows

    暴力解. #include <cstdio> #include <cstring> #define MAXN 105 char map[MAXN][MAXN]; ]; int ...

  4. BZOJ 1059 [ZJOI2007]矩阵游戏

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2707  Solved: 1322[Submit][Stat ...

  5. 2015 CCC - 01 统计数对

    源:CNUOJ-0384 http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=354 题目分析:当时拿到这道题第一个想法就是排序后n^2暴 ...

  6. 线段树(倒序操作):POJ 2828 Buy Tickets

    Buy Tickets   Description Railway tickets were difficult to buy around the Lunar New Year in China, ...

  7. 【贪心】HDU 5783 Divide the Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目 ...

  8. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  9. 加密解密,CryptoStream()的使用

    一:上图 二:代码 主界面代码 using System; using System.Collections.Generic; using System.ComponentModel; using S ...

  10. VS插件 热

    1. AnkhSVN - Subversion SCC Providerhttp://ankhsvn.open.collab.net/ AnkhSVN是一个VS的Subversion 源代码管理提供者 ...