更新和嵌套的select语句?

时间:2011-03-06 15:19:00

标签: sql oracle sql-update

我想更新那些未购买1年的产品的价格。我该怎么做?

我目前的查询是:

UPDATE product 
   SET price = price * 0.9 
 WHERE date_purchase > SYSDATE - 365 
   AND pid IN ([How do i select the items thats not been purchased in 1year??]);

我有两张桌子:

  • 产品=> pid,p_name等...(pid =产品ID,p_name =产品名称)
  • 购买=> pid,date_purchase等

2 个答案:

答案 0 :(得分:2)

我会使用NOT EXISTS,因为它使要求更加透明。

update product 
set price = price * 0.9 
where not exists
  (select 1 from PURCHASE pchase
  WHERE pchase.pid = PRODUCT.pid
  and pchase.date_purchase > add_months(sysdate,-12))

当然,您可能想要考虑如何处理刚刚推出的产品(例如一周之久)并且从未销售过。

答案 1 :(得分:0)

我认为这可能会接近

update product 
set price = price * 0.9 
where  pid NOT IN (
   select pr.pid 
   from product pr
   left outer join purchase pu   
   on pu.pid = pr.pid
   where (( pu.date_purchase != null) 
           AND pu.date_purchase < (SYSDATE - 365))
   or pu.pid == null
);