zoukankan      html  css  js  c++  java
  • 利用带关联子查询Update语句更新数据

          Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到。但update的用法不仅于此,真正在开发的时候,灵活恰当地使用update可以达到事半功倍的效果。

          假定有表Table1(a,b,c)和Table2(a,c),现在Table1中有些记录字段c为null,要根据字段a在Table2中查找,取出字段a相等的字段c的值来更新Table1。一种常规的思路,通过游标遍历Table1中字段c为null的所有记录,在循环体内查找Table2并进行更新,即用游标Cursor的形式。测试sql语句如下:

    使用游标遍历方式更新

     
         虽然用游标可以实现,但代码看起来很复杂,其实用Update根据子关联来更新只要一条语句就可以搞定了,测试代码如下:

    使用带关联子查询的Update更新

     
          参考资料:也许是被忽略的update语句update 子查询

  • 相关阅读:
    check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc
    Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage
    Aspose.words Java基于模板生成word之循环图片
    Aspose.words Java基于模板生成word之纯文本内容
    spring boot之创建web项目并访问jsp页面
    Google APAC----Africa 2010, Qualification Round(Problem B. Reverse Words)----Perl 解法
    Google APAC----Africa 2010, Qualification Round(Problem A. Store Credit)----Perl 解法
    Perl学习笔记(3)----遍历哈希表的一个容易疏忽的地方
    Perl学习笔记(1)----入门
    Perl学习笔记(2)----正则表达式数字匹配的一个疏忽
  • 原文地址:https://www.cnblogs.com/freshman0216/p/1267437.html
Copyright © 2011-2022 走看看