0

I have a very simple SELECT looking like this:

SELECT path FROM items WHERE id = 17

Now in the table items there is also a column count, which I want to have incremented everytime the above query is executed.

What is the optimal way to do this?

clamp
  • 33,000
  • 75
  • 203
  • 299

3 Answers3

4

There is no way to do that in same query.

You can do something like this:

START TRANSACTION;
UPDATE ....
SELECT ....
COMMIT;
RNK
  • 5,582
  • 11
  • 65
  • 133
1

You need to create a stored procedure and then create an atomic transaction using

START TRANSACTION;
UPDATE items set count = (SELECT MAX(count)+1 from items);
SELECT path FROM items WHERE id = 17;
COMMIT;
Sim1
  • 534
  • 4
  • 24
0

try combining the update and select into a single call, the response from the SELECT should be returned

UPDATE items SET count = count + 1; SELECT path FROM items WHERE id = 17
dbinns66
  • 790
  • 6
  • 7