postgresql中的条件插入语句

时间:2017-06-15 21:18:59

标签: 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)
);

这是我得到的错误:

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

1 个答案:

答案 0 :(得分:3)

你不能有两个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));