James Lee
James Lee

Reputation: 11

sql syntax error from my query

I'm recently developing database api using java, vertx, gradle and mysql. I think I made a correct query but it shows me an error. Anybody knows the problem? Thanks in advance.

Query :

INSERT INTO USER VALUES('[email protected]', 'test', 'password')
WHERE NOT EXISTS (SELECT * FROM USER WHERE email='[email protected]')

Error :

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='[email protected]')' at line 1

Upvotes: 0

Views: 73

Answers (2)

Blank
Blank

Reputation: 12378

I think you should do it like this:

INSERT INTO USER
SELECT '[email protected]', 'test', 'password'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM USER WHERE email='[email protected]')

Upvotes: 0

erolkaya84
erolkaya84

Reputation: 1849

I believe you should mention column names after INSERT INTO USER .

Something like this;

INSERT INTO USER (email, username, password)
VALUES('[email protected]', 'test', 'password') 
WHERE NOT EXISTS 
(SELECT * FROM USER WHERE email='[email protected]')

Upvotes: 2

Related Questions