简体   繁体   中英

Conditional insert statement in postgresql

I am trying to run a conditional insert statement but running to issues. Here the statement:

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)
);

This is the error I get:

ERROR:  syntax error at or near "where"
LINE 2: where not exists(
        ^
********** Error **********

ERROR: syntax error at or near "where"
SQL state: 42601
Character: 153

You can't have two where clauses, only one:

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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