关于Update在不同数据库的使用
创始人
2024-08-02 05:41:01
0

1、单表更新多个字段

DB2:

  1. update t1 set (id, name)=('1','2') where name like 'kiss%'--正确运行  
  2. update t1 set id='1',name='2' where name like  'kiss%'--正确运行 

MSSQL:

  1. update t1 set (id, name)=('1','2') where name like 'kiss%'----报: '(' 附近有语法错误。  
  2. update t1 set id='1',name='2' where name like  'kiss%'--正确运行 

2、多表连合更新

DB2:

  1. update t1 set id=(select id from t2 where t2.name like  'kiss%'--正确运行  
  2. update t1 set (id,name)=(select id,name from t2 where t2.name like  'kiss%'--正确运行  
  3. update t1 a set id=b.id from t2 b where b.id='dkdkd'--sql遇到非法符号 

MSSQL:(update tablename 别名,这种写法是不对的)

  1. update t1 set id=b.id, bid=b.bid from t2 b where b.bid='dkdkd' --正确运行  
  2. -----如果要用别名,则也要把t1放在from后面------  
  3. update a set a.id=b.id, a.bid=b.bid from t1 a, t2 b where a.id=b.id and b.bid='dkdkd' --正确运行(别名放在from后) 

综上,更新多个字段,有两种写法,分别是

  1. update tname set (a1, a2...)=(select a1, a2...from...)--DB2写法  
  2. --------------------华丽分割线----------  
  3. update tname set a1=b.a2, a2=b.a2...from b...   --mssql 写法 

Oracle下面跟db2差不多,环境没搭建好,就不测试了,要用的时候再参考以上两种写法.

简单在mysql下测试,结果如下,mysql与mssql都支持 set a1=b.a2, a2=b.a2...from b. 的写法

  1. mysql> update order2 set id=1,price=2 where ordernum='kdkdk' 
  2.     -> ;  
  3. Query OK, 0 rows affected (0.00 sec)  
  4. Rows matched: 0  Changed: 0  Warnings: 0  
  5.  
  6. mysql> update order2 set (id, price)=(1,2) where ordernum='dkdkd' 
  7.     -> ;  
  8. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that  
  9. corresponds to your MySQL server version for the right syntax to use near '(id,  
  10. price)=(1,2) where ordernum='dkdkd'' at line 1 

原文链接: http://shoushou2001.iteye.com/blog/1110350

【编辑推荐】

  1. 客户的一次疏忽,DBA的一次噩梦
  2. 单数据库vs多数据库,单实例vs多实例 效率测试
  3. 一个笔记告诉你,从Java存储转到SQL存储的过程
  4. Oracle数据库日常维护知识总结

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...