整数的无效输入语法:“9Na_(2)SO_(4)”

invalid input syntax for integer: "9Na_(2)SO_(4)"

我正在尝试在 table:

中插入一个字母数字值
INSERT INTO solution (solution, nextsolution) VALUES
    ('9Na_(2)SO_(4)', NULL), ('2Ni(OH)_(3)', (SELECT id FROM solution WHERE solution='9Na_(2)SO_(4)' & nextsolution=null));

solution 是文本类型,nextsolution 是一个整数。不幸的是,postgresql 不允许我执行 WHERE 子句。它给了我错误:

ERROR:  invalid input syntax for integer: "9Na_(2)SO_(4)"
LINE 9: ...OH)_(3)', (SELECT id FROM solution WHERE solution='9Na_(2)SO...

我该如何解决这个问题?

问题是 where 子句中的语句:'9Na_(2)SO_(4)' & nextsolution=null 试图对字符串执行 bitwise and (&) 操作,这将不起作用(并且可能反正不是你想要的)。

查看您的查询,我认为您想要的是首先插入值 '9Na_(2)SO_(4)',然后插入值 '2Ni(OH)_(3)' 以及上一个插入行的 ID。

您需要将此作为两个语句执行并使用不同的语法。这应该可以满足您的要求:

INSERT INTO solution (solution, nextsolution) VALUES (
  '9Na_(2)SO_(4)', 
  NULL
);

INSERT INTO solution (solution, nextsolution) VALUES (
  '2Ni(OH)_(3)', 
  (SELECT id FROM solution WHERE solution='9Na_(2)SO_(4)' and nextsolution is null)
);

您需要使用 AND 而不是 & 来加入您的 WHERE 子句 - 符号 (&) 用于按位运算。