-1

Hi this might be an easy question. but after trying multiple things i gave up.

Cant see what is wrong with this query:

IF NOT EXISTS(SELECT email FROM gebr_tickets WHERE email ='test@test.nl' ) 
INSERT INTO gebr_tickets (name,gebr_name, password, email, RelatieID) 
VALUES ('k','k','cmmNkftA','test@test.nl',170)

the error is :

[Err] 1064 - 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 email FROM gebr_tickets WHERE email ='test@test.nl' ) INSERT INTO'

juergen d
  • 201,996
  • 37
  • 293
  • 362
Flidiot
  • 135
  • 1
  • 1
  • 11
  • http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table this link maybe help you to solve your problem. – Rakibul Hasan Apr 30 '15 at 22:18

3 Answers3

1

You can try the below code for ignoring duplicate records

INSERT IGNORE INTO gebr_tickets (name,gebr_name, password, email, RelatieID) 
VALUES ('k','k','cmmNkftA','test@test.nl',170);
Bhavya Shaktawat
  • 2,504
  • 1
  • 13
  • 11
0

Simplest approach would be

INSERT INTO gebr_tickets (NAME,gebr_name, PASSWORD, email, RelatieID) 
SELECT 'k','k','cmmNkftA','test@test.nl',170 FROM DUAL 
WHERE NOT EXISTS (SELECT email FROM gebr_tickets WHERE email ='test@test.nl' )

This is another way .

DELIMITER $$
CREATE PROCEDURE `sp_insert` ()
BEGIN
IF NOT EXISTS (SELECT email FROM gebr_tickets WHERE email ='test@test.nl' )
THEN


INSERT INTO gebr_tickets (name,gebr_name, password, email, RelatieID) 
VALUES ('k','k','cmmNkftA','test@test.nl',170);


END IF;

END$$
DELIMITER ; 

CALL sp_insert(); 

DROP PROCEDURE IF EXISTS `sp_insert`;
Akhil
  • 2,602
  • 23
  • 36
0

You can't use control statements like IF outside of procedures/functions/triggers. You need a shell around that.

juergen d
  • 201,996
  • 37
  • 293
  • 362