关于Update问题

cysh 2004-12-07 04:11:28
在oracle中如何根据多表的数据来更新一个表?
如在SQL Server中有如下的更新,在oracle中如何实现?
UPDATE titles
SET ytd_sales = titles.ytd_sales + sales.qty
FROM titles, sales
WHERE titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

...全文
174 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
tigerjacky 2004-12-15
  • 打赏
  • 举报
回复
xue xi
cysh 2004-12-08
  • 打赏
  • 举报
回复
用merge是否可以实现?
cysh 2004-12-08
  • 打赏
  • 举报
回复
顶起来!
ORARichard 2004-12-07
  • 打赏
  • 举报
回复
所有的sales都换成(select title_id,sales_id,sum(qty) from table_tmp group by title_id,sales_id)就行了
youxia001 2004-12-07
  • 打赏
  • 举报
回复
CodeMagic 的比较简洁,不过要改一下
Update titles a
set ytd_sales=ytd_sales+
nvl((select qty from sales b where a.title_id=b.title_id),0)
cysh 2004-12-07
  • 打赏
  • 举报
回复
上面少写了个别名,如下:
UPDATE titles
SET ytd_sales = titles.ytd_sales + b.qty
FROM titles a, (select title_id,sales_id,sum(qty) as qty from table_tmp group by title_id,sales_id) b
WHERE titles.title_id = b.title_id
cysh 2004-12-07
  • 打赏
  • 举报
回复
sql server语法:
UPDATE a
SET a.ytd_sales = a.ytd_sales + b.qty
FROM titles a, sales b
WHERE a.title_id = b.title_id
如果B表是一个导出表怎么办:
如:
UPDATE titles
SET ytd_sales = titles.ytd_sales + b.qty
FROM titles a, (select title_id,sales_id,sum(qty) from table_tmp group by title_id,sales_id) b
WHERE titles.title_id = b.title_id
怎么转换成oracle语法?
CodeMagic 2004-12-07
  • 打赏
  • 举报
回复
Update titles a
set ytd_sales=ytd_sales+
nvl((select qty from sales b where a.title_id=b.title_id),a.title_id)
xing_visitor 2004-12-07
  • 打赏
  • 举报
回复
update ...
set column=(select ... from ... where...)
ORARichard 2004-12-07
  • 打赏
  • 举报
回复
update titles a
set a.ytd_sales=(select a.ytd_sales+b.qty from sales b
where a.title_id=b.title_id)
where exists (select 1 from sales b
where a.title_id=b.title_id)
gaoyongjun 2004-12-07
  • 打赏
  • 举报
回复
sorry ,忘记写别名了

UPDATE titles a
SET ytd_sales = a.ytd_sales + (select b.qty from sales b where b.title_id = a.title_id)
WHERE exists (select 1 from sales b where b.title_id = a.title_id );
gaoyongjun 2004-12-07
  • 打赏
  • 举报
回复
UPDATE titles a
SET ytd_sales = a.ytd_sales + (select b.qty from sales where b.title_id = a.title_id)
WHERE exists (select 1 from sales where b.title_id = a.title_id );
cysh 2004-12-07
  • 打赏
  • 举报
回复
楼上两位的我有点看不明白,这样子,麻烦把下面的SQL Server更新,转换成oracle的语法:

UPDATE titles
SET ytd_sales = titles.ytd_sales + sales.qty
FROM titles, sales
WHERE titles.title_id = sales.title_id
qiaozhiwei 2004-12-07
  • 打赏
  • 举报
回复
update titles
set ytd_sales = (select titles.ytd_sales + sales.qty from titles,sales
where titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
zhpsam109 2004-12-07
  • 打赏
  • 举报
回复
学习!
ORARichard 2004-12-07
  • 打赏
  • 举报
回复
update titles a set a.ytd_dales=(select a.ytd_sales+b.qty from sales b where a.title_id=b.title_id and b.ord_date=(select max(ord_date) from sales)) where exists (select 1 from sales b where a.title_id=b.title_id and b.ord_date=(select max(ord_date) from sales)) ;
bluelamb 2004-12-07
  • 打赏
  • 举报
回复
update (select titles.ytd_sales,sales.qty FROM titles, sales
WHERE titles.title_id = sales.title_id
and sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
) set ytd_sales=ytd_sales+qty

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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