简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM