子查询应该始终是select语句吗?

时间:2020-07-21 16:50:53

标签: sql postgresql subquery

select * from product where customer_id = (delete from product where product_id = 5 and customer_id = 1 returning customer_id);

当我在上面运行查询时,我得到:

错误:“来自”或附近的语法错误

但是单独运行delete from product where product_id = 5 and customer_id = 1 returning customer_id可以正常工作,并返回customer_id。

所以问题是子查询是否始终是select语句? 还有其他方法可以在一个查询中进行删除和选择吗?

1 个答案:

答案 0 :(得分:0)

使用CTE:

with d as (
      delete from product
      where product_id = 5 and customer_id = 1
      returning customer_id
     )
select p.*
from product p
where p.customer_id in (select d.customer_id from d);