SQL更新选择语句

时间:2018-11-19 16:27:17

标签: sql postgresql

我正在尝试更新数据库中的条目(PostgreSQL)。我在更新查询时遇到问题:

INSERT INTO prices (price, product_id)
SELECT 
product_price, commn_id
FROM products_temp as prod
WHERE NOT EXISTS 
(SELECT *
   FROM   prices od
   WHERE  prod.commn_id = od.product_id)
OR prod.product_price != 
( SELECT price
  FROM prices as p
  WHERE p.product_id = prod.commn_id
  order by p.end
  desc LIMIT 1 )

当我删除时查询工作正常:

OR prod.product_price != 
( SELECT price
  FROM prices as p
  WHERE p.product_id = prod.commn_id
  order by p.end
  desc LIMIT 1 )

所以在我看来,它正在循环执行此操作。我的问题是,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为您的整个问题都来自prod.product_price !=中的运算符
我尝试过了,对我有用:

INSERT INTO prices (price, product_id)
SELECT 
product_price, commn_id
FROM products_temp as prod
WHERE NOT EXISTS 
(SELECT *
   FROM   prices od
   WHERE  prod.commn_id = od.product_id)
OR prod.product_price not in 
( SELECT price
  FROM prices as p
  WHERE p.product_id = prod.commn_id
  order by p.end
  desc LIMIT 1 )

希望它将对您有帮助!