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更新
  • 相关阅读:
    用spring tool suite插件创建spring boot项目时报An internal error occurred during: "Building UI model". com/google/common/
    Eclipse卸载插件SpringSoource-tool-suite
    spring-tool-suite使用教程,并创建spring配置文件
    MySQL获取指定长度的字符串的函数left(s,n)和right(s,n)
    maven web项目配置log4j,及log4j参数设置
    Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'zoneId' in 'class java.lang.String'
    ifconfig 输出里没有IP地址
    maven web项目生成WebContent或WebRoot目录
    Window 命令行神器:cmder
    Filezilla Download for Linux (deb, rpm, txz, xz, amd64, i386, i486, i586, i686, x86_64)
  • 原文地址:https://www.cnblogs.com/Fandyx/p/2023294.html
Copyright © 2011-2022 走看看