1

I am trying to figure out how do to a conditional Update else Insert, however I am having issue with my query and cannot find the issue. Any tips or help would be appreciated.

IF EXISTS (SELECT * FROM analytics WHERE visit_date="09-10-2019") THEN
    UPDATE analytics
    SET slug = "000aaa", visit_date="09-10-2019", visits="1", continent="NA", country="Canada", state="Quebec", city="Montreal"
    WHERE visit_date="09-10-2019";
ELSE
    INSERT INTO analytics (slug, visit_date, visits, continent, country, state, city)
    VALUES ("000aaa","09-10-2019", "1", "NA", "Canada", "Quebec", "Montreal");
END IF;

Error : You have an error in your SQL syntax; it seems the error is around: 'IF EXISTS ( SELECT * FROM analytics WHERE visit_date = "09-10-2019") at line 1

Eric E
  • 572
  • 1
  • 4
  • 16
  • Any error reported? – Robert Kock Oct 04 '19 at 15:33
  • Yes, I added it to question for everyone to see. – Eric E Oct 04 '19 at 15:34
  • INSERT INTO analytics (slug, visit_date, visits, continent, country, state, city) VALUES ("000aaa","09-10-2019", "1", "NA", "Canada", "Quebec", "Montreal") ON DUPLICATE KEY UPDATE slug="000aaa", visit_date="09-10-2019", visits = "1", continent = "NA", country = "Canada", state = "Quebec", city = "Montreal" – Zhurov Konstantin Oct 04 '19 at 17:30
  • 1
    'MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT constructs for flow control within stored programs' https://dev.mysql.com/doc/refman/8.0/en/flow-control-statements.html – P.Salmon Oct 05 '19 at 06:57

0 Answers0