mysql多表关联更新


当前第2页 返回上一页

如果舍弃where条件,则默认对A表进行全表

更新,但由于(select b.city_name from tmp_cust_city b where where b.customer_id=a.customer_id)

有可能不能提供"足够多"值,因为tmp_cust_city只是一部分客户的信息,

所以报错(如果指定的列--city_name可以为NULL则另当别论):

01407, 00000, "cannot update (%s) to NULL"

// *Cause:

// *Action:

替换的方法:

1

2

3

4

5

update customers a -- 使用别名

set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)

或者

set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')

-- 当然这不符合业务逻辑了

一个比较简便的方法就是将A表代入 值表达式 中,使用group by 和having 字句查看重复的纪录。

1

2

3

4

5

6

(select b.customer_id,b.city_name,count(*)

 from tmp_cust_city b,customers a

 where b.customer_id=a.customer_id

 group by b.customer_id,b.city_name

 having count(*)>=2

)

以上就是mysql多表关联更新的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

了解mysql如何优化

mysql数据库事务transaction示例讲解教程

mysql索引介绍

mysql查看数据库命令是什么?

浅谈mysql jdbc streamresult通信原理

mysql数据库的id不递增怎么办

order是什么意思?

关于mysql元数据如何生成hive建表语句注释脚本

mysql 数据库函数库

windows10安装mysql5.7.18教程

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...