我的查询中的SQL语法错误

时间:2016-08-08 08:33:00

标签: java mysql

我最近使用java,vertx,gradle和mysql开发数据库api。我想我做了一个正确的查询,但它显示了一个错误。有人知道这个问题吗?提前谢谢。

查询:

INSERT INTO USER VALUES('test@test.co.kr', 'test', 'password')
WHERE NOT EXISTS (SELECT * FROM USER WHERE email='test@test.co.kr')

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS (SELECT * FROM USER WHERE email='test@test.co.kr')' at line 1

2 个答案:

答案 0 :(得分:2)

我相信你应该在INSERT INTO USER之后提及列名。

像这样;

INSERT INTO USER (email, username, password)
VALUES('test@test.co.kr', 'test', 'password') 
WHERE NOT EXISTS 
(SELECT * FROM USER WHERE email='test@test.co.kr')

答案 1 :(得分:0)

我认为你应该这样做:

INSERT INTO USER
SELECT 'test@test.co.kr', 'test', 'password'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM USER WHERE email='test@test.co.kr')