Vivek Goel
Vivek Goel

Reputation: 24150

Update statement followed by select MySQL

I am storing cache information from the cache server if application version is matching else I need to set cache details as NULL.

Currently I am doing it like this

UPDATE 
  cache_table 
SET
  _data = NULL 
WHERE _id = id AND _app_version != "current_version" 

Followed by select query

SELECT 
      _data 
    FROM
      cache_table 
    WHERE _id = id AND _app_version == "current_version" 

Is there a way I can do required update and select in one query without firing two query ?

Note: I don't want to use MySQL procedure. No specific reason but don't want to store application logic in DB so I can easily change database application.

Upvotes: 0

Views: 125

Answers (2)

tadman
tadman

Reputation: 211590

Generally UPDATE and SELECT are two distinct operations. The only way to combine them is with a stored procedure as you identify.

Upvotes: 1

Vikram
Vikram

Reputation: 4190

I dont believe in MySQL there is a built-in way to do this. However, just FYI this can be done in SQL Server using OUTPUT clause:

http://msdn.microsoft.com/en-us/library/ms177564.aspx

Upvotes: 0

Related Questions