0

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('test@test.co.kr', 'test', 'password')
WHERE NOT EXISTS (SELECT * FROM USER WHERE email='test@test.co.kr')

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='test@test.co.kr')' at line 1
singhakash
  • 7,613
  • 4
  • 25
  • 59
James Lee
  • 11
  • 4

2 Answers2

2

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

Something like this;

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')
erolkaya84
  • 1,454
  • 18
  • 24
0

I think you should do it like this:

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')
Blank
  • 11,958
  • 1
  • 11
  • 30