繁体   English   中英

更新 Postgres 中左关节表的 NULL 值

[英]update NULL values of left joint table in Postgres

我的数据库文档和位置中有这两个表。

select * from  Document     

id  locid   loccode
1   1010    30
2   2020    30
3   3030    30
4   4040    40

select * from location

locid   loccode date    Status
1010    30  20-10-2019  A
2020    30  20-10-2019  A
3030    40  20-10-2019  A
4040    40  20-10-2019  A
6060    30  20-10-2019  A
7070    40  20-10-2019  A
8080    30  20-10-2019  D
9090    40  20-10-2019  D

我想更新位置表中的状态,其记录(左连接 null 值)不可用文档。 我尝试以下查询,但需要更多时间。

update location 
set status='D' 
from location A 
left join document B on A.locid=B.locid and A.loccode=B.loccode  
where b.id is NULL;

请帮助我如何解决它

我建议not exists

update location 
set status = 'D' 
where not exists (
    select 1 
    from document d
    where d.locid = location.locid and d.loccode = location.loccode
)

出于性能考虑,您需要在document(locid, loccode)上建立索引,以便子查询快速执行。 location(locid, loccode)也可能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM