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
Solved

Insert if not exists else update mysql

Posted on 2010-09-22
8
1,563 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
  • 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 250 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: Do I need CLUSTERED here? 13 59
Stored Proc - Rewrite 42 59
IIF in access query 19 24
Generate PDF from MySQL using PHP 3 19
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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