如何使用表之间的某些连接编写更新查询

时间:2014-06-05 14:29:08

标签: sql oracle join updating

我想如何编写下面我刚才写的SQL(Oracle)的更新查询 按如下所示进行更新,使用多个表(其中3个):

   update payroll.emp_appointments
   set e.sales=w.sales
   where the details below

   select w.storecode,w.salespersoncode,w.sumsales,w.truncateddate, c.customerid, 
   e.firstname,e.lastname,c.contactid
   from weekstat w, sysdba.contact_retail c, payroll.emp_appointments e where 
   c.customerid=w.customerid
   and cast(w.salespersoncode  as char(6))=e.emp_num
   and trunc(e.tradingdate)=w.truncateddate
   and e.contactid=c.contactid
   and e.storecode=cast(e.storecode as varchar2(6))
   and e.sales=0.00
   and e.tradingdate > sysdate-10

我在创建临时表后尝试执行以下操作,但它似乎更新了emp_appointments表中我不想做的所有行:

   update payroll.emp_appointments e
   set e.sales= (
   select ue.sumsales
   from update_emp_sales_temp ue
   where e.contactid=ue.contactid and
   e.emp_num=ue.salespersoncode and 
   e.storecode=ue.storecode and
   e.firstname=ue.firstname and 
   e.lastname=ue.lastname and
   trunc(e.tradingdate)=ue.truncateddate)
   where e.tradingdate>sysdate-30;

2 个答案:

答案 0 :(得分:0)

更容易记住构造

update <select with all the joins> set <set conditions>

在你的情况下:

UPDATE
(select w.sales src, e.sales dst
from weekstat w, sysdba.contact_retail c, payroll.emp_appointments e where 
c.customerid=w.customerid
and cast(w.salespersoncode  as char(6))=e.emp_num
and trunc(e.tradingdate)=w.truncateddate
and e.contactid=c.contactid
and e.storecode=cast(e.storecode as varchar2(6))
and e.sales=0.00
and e.tradingdate > sysdate-10)
SET dst = src

请记住,您只需要更新一个表


使用MERGE的另一个方法:

MERGE INTO payroll.emp_appointments e
USING (select w.sales, e.emp_num
from weekstat w, sysdba.contact_retail c, payroll.emp_appointments e where 
c.customerid=w.customerid
and cast(w.salespersoncode  as char(6))=e.emp_num
and trunc(e.tradingdate)=w.truncateddate
and e.contactid=c.contactid
and e.storecode=cast(e.storecode as varchar2(6))
and e.sales=0.00
and e.tradingdate > sysdate-10) subquery
ON (e.emp_num = subquery.emp_num)
WHEN MATCHED THEN UPDATE SET e.sales = subquery.sales;

我没有足够的信息来详细了解你的逻辑修复上面的查询,所以这里是第三个选项:)(它也可能会失败,更多关于它的底部):

UPDATE payroll.emp_appointments e
SET 
sales = (select w.sales from weekstat w, sysdba.contact_retail c
where 
c.customerid=w.customerid
and cast(w.salespersoncode  as char(6))=e.emp_num
and trunc(e.tradingdate)=w.truncateddate
and e.contactid=c.contactid) 
)
WHERE e.sales=0.00
and and e.tradingdate > sysdate-10
and (e.emp_num, trunc(e.tradingdate), e.contactid)
in 
(select w.salespersoncode, w.truncateddate, c.contactid from 
weekstat w, sysdba.contact_retail c
where c.customerid=w.customerid)

我不喜欢它因为你基本上重复逻辑两次。你可能会在'sales ='之后选择一个错误,它没有返回比例值,但我没有足够的信息来解决这个问题。因此,您有责任确保该选项仅为表e中将要更新的行中的任一行返回1行。

此外,我删除了看起来多余的条件and e.storecode=cast(e.storecode as varchar2(6))

答案 1 :(得分:0)

谢谢Vav,我尝试了以下内容,现在它起作用了:

UPDATE payroll.emp_appointments e
SET (e.sales) = (
SELECT w.sumsales
FROM weekstat w, sysdba.contact_retail c
where 
    c.customerid=w.customerid
   and cast(w.salespersoncode  as char(6))=e.emp_num
   and trunc(e.tradingdate)=w.truncateddate
   and e.contactid=c.contactid
   and e.storecode=cast(e.storecode as varchar2(6))
   and e.sales=0.00
   and e.tradingdate > sysdate-10)

where exists (SELECT w.sumsales
FROM weekstat w, sysdba.contact_retail c
where 
    c.customerid=w.customerid
   and cast(w.salespersoncode  as char(6))=e.emp_num
   and trunc(e.tradingdate)=w.truncateddate
   and e.contactid=c.contactid
   and e.storecode=cast(e.storecode as varchar2(6))
   and e.sales=0.00
   and e.tradingdate > sysdate-10)