If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Can I combine these two update statements?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-21-09, 12:41
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Can I combine these two update statements?

I have a suspicion that the answer is "no", but I never found out nuffink by not asking, so:

Code:
UPDATE Greetings SET openDate = now() WHERE idGreetings = 123 AND openDate IS NULL

UPDATE Greetings SET greetingOpenCount = (greetingOpenCount+1) WHERE idGreetings = 123
My application logic has forced me into a position where I'm running those two straight after each other: I want the "open" count incremented each time a greeting is opened, but I only want to insert the date if it's not already set.

Is there any way to turn my two DB calls into one?

Thanks
Reply With Quote
  #2 (permalink)  
Old 05-21-09, 14:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by Spudhead
I have a suspicion that the answer is "no"
your suspicionating was incorrect
Code:
UPDATE Greetings 
   SET greetingOpenCount = greetingOpenCount + 1 
     , openDate = CASE WHEN openDate IS NULL
                       THEN CURRENT_TIMESTAMP
                       END
 WHERE idGreetings = 123
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On