PostgreSQL中不存在语法错误 [英] WHERE NOT EXISTS in PostgreSQL gives syntax error

查看:204
本文介绍了PostgreSQL中不存在语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用 WHERE NOT EXISTS 子句来防止在 age ,我在 WHERE处或附近收到错误语法错误。

When trying to use the WHERE NOT EXISTS clause to prevent adding a row with a duplicate value in the column age, I get the error syntax error at or near "WHERE".

为什么会引发语法错误?我正在使用Postgresql 9.1。

Why did it throw a syntax error? I'm using Postgresql 9.1.

SQL

INSERT INTO live.users ("website", "age") 
values ('abc', '123')
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);

错误

ERROR:  syntax error at or near "WHERE"
LINE 6: WHERE NOT EXISTS (SELECT age FROM live.users W...


推荐答案

执行以下操作:

INSERT INTO live.users ("website", "age") 
SELECT 'abc', 123
WHERE NOT EXISTS (SELECT age FROM live.users WHERE age = 123);

这篇关于PostgreSQL中不存在语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆