【发布时间】:2016-06-12 04:40:09
【问题描述】:
我有一个log 表如下:
id status1 status2 ref dateofchange
1 10 12 33 1.1.10
2 10 12 34 1.1.15
3 5 8 14 1.5.10
4 10 12 33 2.1.10
还有一张表tab如下:
id ref qty commitdate
1 17 5 1.1.10
2 33 8 1.1.10
3 34 7 1.12.14
4 34 8 1.2.16
5 34 8 1.1.15
我有一个查询,它给了我来自log 表的行:
select *
from log
where status1=10 and status2=12
这给出了:
id status1 status2 ref dateofchange
1 10 12 33 1.1.10
2 10 12 34 1.1.15
4 10 12 33 2.1.10
对于这些行中的每一行,我想从标签中删除 log.ref=tab.ref and tab.commitdate<=log.dateofchange 的所有行
删除标签表后应如下所示:
id ref qty commitdate
1 17 5 1.1.10
4 34 8 1.2.16
我尝试使用 WITH 查询来做到这一点:
With l as (
select *
from log
where status1=10 and status2=12
)
delete from tab where l.ref=tab.ref and tab.commitdate<=l.dateofchange
但是这不起作用。
错误:表“l”缺少 FROM 子句条目
我该怎么做?
【问题讨论】:
标签: sql postgresql