我有三个表一个叫facility
,一个hotel
,另一个hotel_facility
是连接表的酒店和娱乐设施。到目前为止,一切都很好。facility
桌子上有很多重复的东西。我想更新联接表,然后删除表中的重复项facility
。查找重复项并将其删除不是问题:
select o.id
from facility o
where exists ( select 'x' from facility i where i.name = o.name);
然后根据此查询删除 但是如何更新联接表,我将如何使用最新版本的Postgres。我可以使用其他表中的最新值来更新表吗?
谢谢
我认为您需要先进行更新,然后再进行删除。此查询会将设施ID设置为该名称的最低设施ID:
update hotel_facility hf
set facility_id = (select min(f.facility_id)
from facility f join
facility f2
on f.name = f2.name
where f.id = hf.facility_id);
然后,您可以删除除最小值以外的所有内容:
delete from facility f
where exists (select 1
from facility f2
where f2.name = f.name and f2.id > f.id
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句