试图在 SQL 中运行“WHERE IS NOT NULL”查询但收到错误?

问题描述

我觉得我遗漏了一些明显的东西,但我正在尝试在 pg-promise 中为 node/express 运行这个命令:

const condition = pgp.as.format(" WHERE value IS NOT null",values);
const query = pgp.helpers.insert(values,cs) + condition;

翻译过来就是:

INSERT INTO "formquestionresponse"("attrib_id","value","response_id") VALUES(63,'Philadelphia Flyers','135'),(64,'Test',(68,null,(77,'135') WHERE value IS NOT null

但我收到一个 sql 错误 16:03:46 错误:“WHERE”处或附近的语法错误

但我终其一生都无法弄清楚如何解决这个问题……我是否遗漏了一些明显的东西?

感谢您的帮助!

解决方法

INSERT ... VALUES ... 语句不能有 WHERE 子句。

如果您不想使用 INSERT ... VALUES ... 语句插入某些元组,请不要将它们列在 VALUES 子句中。

您可以做的是 INSERT ... SELECT ... FROM (VALUES ...) ... 声明:

INSERT INTO formquestionresponse
            (attrib_id,value,response_id)
            SELECT attrib_id,response_id
                   FROM (VALUES (63,'Philadelphia Flyers','135'),(64,'Test',(68,NULL,(77,'135') v (attrib_id,response_id)
                   WHERE value IS NOT NULL;

但我不认为这是更容易或更好的选择。