sql - postgresql中的条件插入语句

标签 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

最佳答案

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

关于sql - postgresql中的条件插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44577197/

相关文章:

c# - 我已经插入了一行,我想获取它的 ID 并加上一个 int 并插入到该行中

查询中的 SQL 动态

java - SQLException pg_get_serial_sequence 未找到

sql - 复合类型的多个插入

Postgresql 函数循环遍历输入参数并追加到 hstore

mysql - sql:使用 not in 并且不选择任何内容

php - 在查询内进行另一个查询时仅显示一行

SQL:指定列的冗余 WHERE 子句 > 0?

sql - Postgres - 聚合分钟到小时

sql - 如何从 PostgreSQL 中的 sql 脚本导入外部 sql 脚本?