自联表怎么更新的问题。

JavaNet的牛肉粉丝 2012-08-16 02:53:28
问题就是将7月字段abcclass为null的值,替换成3月该字段的abcclass值



就是一个产品表p_data,每个月都插入一次这个月的产品数据,我写的sql通不过

update

//2012年7月有很多abcclass字段是null的
(select * from p_data a where a.year=2012 and a.month=7 and a.abcclass is null) y

join

//2012年3月大多数abcclass字段是有值的,就以3月为准
(select * from p_data a where a.year=2012 and a.month=3) x

on(x.productcode=y.productcode)

set y.abcclass=x.abcclass //将7月为null的abcclass字段等于3月份abcclass字段
...全文
114 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ORAClE SE 2012-08-17
  • 打赏
  • 举报
回复
update p_data
set abcclass=(select abcclass from p_data a where p_data.productcode=a.productcode and a.year=2012 and a.month=3)
where p_data.year=2012 and p_data.month=7 and p_data.abcclass is null;
Kobayashi 2012-08-17
  • 打赏
  • 举报
回复
update p_data a
set abcclass=(select abcclass from p_data a where p_data.productcode=a.productcode and a.year=2012 and a.month=3) b
where a.year=2012 and a.month=7 and a.abcclass is null and a.id = b.id;

上面的ID都是你查询出来的同一条记录的值。
风雨天一 2012-08-16
  • 打赏
  • 举报
回复
一楼正解
人生无悔 2012-08-16
  • 打赏
  • 举报
回复

--try
update p_data
set abcclass=(select abcclass from p_data a where p_data.productcode=a.productcode and a.year=2012 and a.month=3)
where p_data.year=2012 and p_data.month=7 and p_data.abcclass is null;

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧