SQL update and selection in one query

Is there a way in SQL (MySQL) to increase the value and also return the value in a single query. I am trying to ovally execute two queries, for example:

QUERY 1

UPDATE my_table SET my_col = (my_col + 1) WHERE something = something_else; 

QUERY 2

 SELECT my_col FROM my_table WHERE something = something_else; 

Thanks.

+4
source share
3 answers

As far as I know, there is no such possibility in MySQL, but look at this question for a possible workaround, which at least allows you to choose and update to work with the same data in a transaction.

+2
source

It is not possible to select and update at the same time. If you want to avoid the selection, you can declare a variable and put the value there, but this will put the last updated value of the string into the variable.

 declare @value int UPDATE my_table SET my_col = (my_col + 1), @value = (my_col + 1) WHERE something = something_else; 
0
source

I don’t know which scripting language you are using, but here is an example of creating a stored procedure in MySQL that returns an updated value so that you can update and select in one operation:

Get updated value in MySQL instead of affected rows

0
source

Source: https://habr.com/ru/post/1390813/


All Articles