Mysql update if

this is the query i am trying to use.

  SET @id = 289;
    SET @feedId = 5;
    SET @statusid = 2;
    SET @processId = 1;

    SET @feedIdExists = (
     SELECT EXISTS (
       SELECT *
       FROM feed_statuses
       WHERE FeedId = @feedId));

    IF feedIdExists = 1 THEN
     UPDATE `snap`.`feed_statuses` 
      SET
      `ProcessId` = @processId , 
      `StatusId` = @statusId , 
      `ProcessEndDate` = NOW()
      WHERE
      `FeedId` = @feedId;
    ELSE
     INSERT INTO `snap`.`feed_statuses` 
      (`FeedId`, 
      `ProcessId`, 
      `StatusId`, 
      `ProcessStartDate`
      )
      VALUES 
      (@feedId,
      @processId,
      @statusId,
      NOW());
    END IF;

hi guys, there are 3 errors about the if syntax. i didnt understand. why doesnt it work properly?

9 queries executed, 6 success, 3 errors, 0 warnings

Query: use snap

0 row(s) affected

Execution Time : 0.001 sec Transfer Time : 0.001 sec

Total Time : 0.002 sec

Query: SET @id = 289

0 row(s) affected

Execution Time : 0.001 sec Transfer Time : 0 sec

Total Time : 0.001 sec

Query: SET @feedId = 5

0 row(s) affected

Execution Time : 0.002 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: SET @statusid = 2

0 row(s) affected

Execution Time : 0.002 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: SET @processId = 1

0 row(s) affected

Execution Time : 0.002 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: SET @feedIdExists = ( SELECT EXISTS ( SELECT * FROM feed_statuses WHERE FeedId = @feedId))

0 row(s) affected

Execution Time : 0.002 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: IF feedIdExists = 1 THEN UPDATE snap.feed_statuses SET ProcessId = @processId , StatusId = @statusId , ProcessEndDate ...

Error Code: 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 feedIdExists = 1 THEN UPDATE snap.feed_statuses SET `Pr' at line 1

Execution Time : 0 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: ELSE INSERT INTO snap.feed_statuses (FeedId, ProcessId, StatusId, ProcessStartDate ) VALUES (@feedId, @processId, @s...

Error Code: 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 'ELSE INSERT INTO snap.feed_statuses (FeedId, `ProcessId' at line 1

Execution Time : 0 sec Transfer Time : 0 sec

Total Time : 0.002 sec

Query: END IF

Error Code: 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 'END IF' at line 1

Execution Time : 0 sec Transfer Time : 0 sec Total Time : 0.002 sec

Answers


You can use Insert ... ON DUPLICATE UPDATE to resolve this issue.

Note: you must have UNIQUE key first, And I think you have it.

INSERT INTO `snap`.`feed_statuses` 
  (`FeedId`, 
  `ProcessId`, 
  `StatusId`, 
  `ProcessStartDate`
  )
  VALUES 
  (@feedId,
  @processId,
  @statusId,
  NOW());
ON DUPLICATE KEY
UPDATE
  `ProcessId` = @processId , 
  `StatusId` = @statusId , 
  `ProcessEndDate` = NOW()

You must check this: INSERT...ON DUPLICATE KEY


Need Your Help