有没有办法删除和返回子查询的值

时间:2018-04-26 07:06:32

标签: postgresql subquery common-table-expression

我希望能够返回子查询的值并删除某些记录。具体来说,只有当所有记录都没有used = True并且还返回使用的记录数时,我才希望从下表中删除。

csv_files
----------|-------
filename  | used  | somevalue |
-------------------------------
test.csv  | t     | 67
-------------------------------
test.csv  | f     | 92
-------------------------------
test.csv  | f     | 92
-------------------------------

我带来了以下内容:

with used_count as (
   select count(used) 
   from csv_files 
   where filename ='test.csv' 
     and used = 't'
), delete as (
   delete from csv_files 
   where filename = 'test.csv' 
    and (select count from used_count) = 0
) 
select count 
from used_count;
count
-------
  1     
(1 row)

在Postgres中有更简单的方法吗?

0 个答案:

没有答案