postgresql 中的条件插入语句

des*_*Joe 0 sql postgresql

我正在尝试运行条件插入语句,但遇到问题。声明如下:

insert into category_content (category_id, content_id, content_type_id, priority) (select 29, id, 1, 1 from article where blog_id = 80) 
where not exists(
select * from category_content where category_id = 29 and firstname in (select id from article where blog_id = 80)
);
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

insert into category_content (category_id, content_id, content_type_id, priority) (select 29, id, 1, 1 from article where blog_id = 80) 
where not exists(
select * from category_content where category_id = 29 and firstname in (select id from article where blog_id = 80)
);
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 6

你不能有两个 where 子句,只有一个:

insert into category_content (category_id, content_id, content_type_id, priority) 
select 29, id, 1, 1 
from article 
where blog_id = 80
  and not exists(select * 
                 from category_content 
                 where category_id = 29 
                   and content_id in (select id 
                                      from article 
                                      where blog_id = 80));
Run Code Online (Sandbox Code Playgroud)