我有这样的表 1:
模板表格:
ID int
Name string
TemplateGroupID int
模板组表:
ID int
Name string
IsDefault bool
我想删除模板中的一行,如果该行的 FK 到 template_group 列 IsDefault
设置为 true。我怎样才能做到这一点?我以为是这样的:
delete from template
where id =33 and exists (
select * from template_group where is_default = true
);
但我认为这是不正确的,因为我不必在子选择中选择 TemplateGroupId
吗?
最佳答案
你可以使用 Postgres' delete ... from ... using ... where ...
syntax :
delete
from template t
using template_group g
where g.ID = t.TemplateGroupID and g.IsDefault = true
关于sql - 如果外键的列设置为 false,Postgres 将删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059915/