?
Solved

Insert if not exists else update mysql

Posted on 2010-09-22
8
Medium Priority
?
1,582 Views
Last Modified: 2012-05-10
Hello.  I have been looking for tihs everywhere.  im trying to run this query

IF NOT EXISTS (SELECT * FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid='1') THEN
	INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
ELSE
	UPDATE tutorials SET keywordlist = "HELLO WORLD" WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid = '1'

Open in new window


this is giving me the error

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 NOT EXISTS (SELECT * FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND ' at line 1

and i cant find out what is wrong with the syntax.

For reasons i dont want to go into i can not use anything that uses duplicate key to accomplish this or the REPLACE

Thank You!
0
Comment
Question by:bretterer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33738708
MySql has something called on duplicate which you can use

basically insert the record and if duplicate key error occurs then perform the update

INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
ON DUPLICATE KEY UPDATE SET keywordlist = "HELLO WORLD"

Its something along those lines anyways. your dup key index would need to be setup correctly I guess

backup table/db just in case it didnt quite go according to plan
0
 
LVL 3

Author Comment

by:bretterer
ID: 33738737
rockiroads,  Thankyou for your quick response but let me point out what i said before

and i quote
" i can not use anything that uses duplicate key to accomplish this"
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 1000 total points
ID: 33738888
ok, sorry.

well only other thing is perhaps a stored procedure within mysql or use some other app to run mysql commands eg vb/ado

mysql stored procedure - something along these lines, cant remember exact syntax
try maybe use a variable then check that (eg cnt)


create procedure myproc
begin
    SELECT count(*) into cnt FROM tutorials WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid='1'
    if cnt = 0 then
        INSERT INTO tutorials (tutorialid, keywordlist, subjectid) VALUES ('Alg1_01_02_0001', 'Hello World', '1')
    else
        UPDATE tutorials SET keywordlist = "HELLO WORLD" WHERE tutorialid = 'Alg1_01_02_0001' AND subjectid = '1'
    end if
end
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 3

Author Comment

by:bretterer
ID: 33738925
Error Code : 1327
Undeclared variable: cnt
(0 ms taken)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33739635
can u try defining the variable, after begin, before select

DECLARE cnt INT DEFAULT 0
0
 
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 1000 total points
ID: 33743427
Try this.

REPLACE INTO `tutorials`
SET `tutorialid` = 'Alg1_01_02_0001',
`keywordlist` = 'Hello World',
`subjectid` = '1';

got from
http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html
0
 
LVL 3

Author Comment

by:bretterer
ID: 33745168
Brichsoft.  That did do it although i said i didnt want to use replace.   becuase of database structure.  I did modify database and it worked.  
0
 
LVL 3

Author Closing Comment

by:bretterer
ID: 33745193
Thanks
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question