Sum in sql update

Asked

Viewed 4,463 times

2

I am running the following sql on my mysql:

UPDATE `cadastro` SET `pontos`= 'pontos + 1' WHERE id = 102;

I need it every time it is Update in the table register the field dots who’s kind int(11) receive +1;

When I spin this sql it does not return me error, however, the field pontos is always at 1.

2 answers

6


You’re trying to add a string, try it the way below:

UPDATE `cadastro` SET `pontos` = `pontos` + 1 WHERE id = 102;

Remember that to work the column points must be of a numeric type. Example: int, double, float, etc....

  • Perfect friend, thank you.

3

Solution to a scenario uncompetitive:

UPDATE cadastro SET pontos = pontos + 1 WHERE id = 102;

A very important consideration when doing this type of update is the competition of write accesses in the table cadastro.

If two clients execute the UPDATE above simultaneously, the data contained in the field pontos runs the risk of not remaining as expected.

The Solution to a Scenario in competition with would that be:

START TRANSACTION;

-- Recupera O valor para atualização
SELECT pontos FROM cadastro WHERE id = 102 FOR UPDATE;

-- Incrementa o contador
UPDATE cadastro SET pontos = pontos + 1 WHERE id = 102;

COMMIT;
  • In its way is giving the following error: #1064 - Você tem um erro de sintaxe no seu SQL próximo a 'LIMIT 0, 25' na linha 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.