错误:“SELECT”处或附近的语法错误

时间:2023-01-06 19:15:36

I am really new to postgres. The question looks very simple but I just cant see where I got wrong.

postgres我真的很新。这个问题看起来非常简单,但我无法看出我的错误。

I a table created as follows:

我创建的表如下:

  CREATE TABLE IF NOT EXISTS t(
        tn VARCHAR(30) NOT NULL,
        PRIMARY KEY(tn)
    );

I want to insert an instance if the instance does not exist. Here is my code:

如果实例不存在,我想插入一个实例。这是我的代码:

INSERT INTO t (tn) 
VALUES 
(SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q')) ;

And the psql console keeps giving me the error

并且psql控制台一直给我错误

ERROR:  syntax error at or near "SELECT"

I have checked every piece of code individually, for instance both

我已经单独检查了每一段代码,例如两者

SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');

and

INSERT INTO t (tn) VALUES ('p');

run without error. But error occurs when I put them together.

运行没有错误。但是当我把它们放在一起时会发生错误。

Does anyone know where I got wrong..?

有谁知道我哪里错了..?

1 个解决方案

#1


4  

Lose VALUES and the brackets...

丢失VALUES和括号......

INSERT INTO t (tn) 
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');

#1


4  

Lose VALUES and the brackets...

丢失VALUES和括号......

INSERT INTO t (tn) 
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');