1

The following SQl query gives me the error:

IF EXISTS (SELECT * FROM comments WHERE user_id='2' AND course_id='1') 
UPDATE comments SET page1='exists' WHERE user_id='2' AND course_id='1' 
ELSE 
INSERT INTO comments (user_id,course_id,page1) VALUES ('2','1','inserted')

" #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 EXISTS (SELECT * FROM comments WHERE user_id='2' AND course_id='1') UPDATE co' at line 1 "

I'm sure the syntax is correct?!

3
  • 1
    Are you trying to do this as a standalone piece of SQL (in which case having a unique key on user_id and course_id , then using INSERT....ON DUPLICATE KEY UPDATE would be easier), or as lines within a stored procedure? Commented Jul 9, 2014 at 10:14
  • I am trying to run this as a SQL query via phpMyAdmin Commented Jul 9, 2014 at 10:16
  • Then that is not valid SQL. IF is a flow control statement, and with a raw SQL query there is no flow to control. Commented Jul 9, 2014 at 10:21

2 Answers 2

2

Add a unique key covering user_id and course_id

Then just use

INSERT INTO comments (user_id,course_id,page1) 
VALUES ('2','1','inserted')
ON DUPLICATE KEY UPDATE page1='exists'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kickstart that works perfect in phpMyAdmin. Although using this SQL statement in php doesn't seem to work. Inserting works but i assume its the duplicate key part that's causing the problems. Is there anyway around this? Sorry i thought if it works in phpMyAdmin it would also work in php.
1

Check this IF-statement syntax:

Try this:

IF EXISTS (SELECT * FROM comments WHERE user_id='2' AND course_id='1') THEN 
    UPDATE comments SET page1='exists' WHERE user_id='2' AND course_id='1';
ELSE 
    INSERT INTO comments (user_id,course_id,page1) VALUES ('2','1','inserted');

OR

As per me you have to make practice of BEGIN...END block as shown below.

IF EXISTS (SELECT * FROM comments WHERE user_id='2' AND course_id='1') THEN 
BEGIN 
    UPDATE comments SET page1='exists' WHERE user_id='2' AND course_id='1';
END 
ELSE 
BEGIN 
    INSERT INTO comments (user_id,course_id,page1) VALUES ('2','1','inserted');
END

My above queries will work in Procedures(PL-SQL). If you want to use above query in SQL then use below code:

Create a UNIQUE constraint on your user_id & course_id columns, if one does not already exist:

ALTER TABLE comments ADD UNIQUE (user_id,course_id);

Use INSERT ... ON DUPLICATE KEY UPDATE:

INSERT INTO comments (user_id,course_id,page1) 
VALUES ('2','1','inserted') 
ON DUPLICATE KEY UPDATE page1='exists'

3 Comments

I don't think that is valid MySQL syntax
Did you try it? There is no IF statement in SQL (and neither in MySQL's SQL dialect)
Thanks for you efforts Saharsh however this still doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.