dimanche 27 août 2017

SQL update if exist, insert if not exist not working

I want to update a record if it exists or create a new record if it does not exist yet but somehow it is throwing an error and I cannot find out why.. I want to update the status of a customer if it exists or create a new customer if not exist.

my query:

$sql = "SELECT CASE WHEN EXISTS ( SELECT * FROM sms WHERE number = '123456789' AND customer_id = '1' ) THEN UPDATE sms SET stat = '1' ELSE INSERT INTO sms (number, customer_id, stat) values ('+32485386902', '1', '1') END"

This throws error: [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 'UPDATE sms SET stat = '1' ELSE INSERT INTO sms (number, customer_id, stat) value' at line 1

However if I change the update and insert into to 1 and 2 it shows 1 if exist, or 2 if not.

Can anyone help me out :) ? Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire