Subtract -1 from the mysql field’s own value

Asked

Viewed 31 times

-2

In a table field of the integer type stores the quantity of items in stock, when I lower, I must give -1 of the field itself, how to do this straight from mysql?

Is that correct?

UPDATE `tblQtd` SET `qtdItens` = (`qtdItens`-1) WHERE `idItenRef` = '".$id."' LIMIT 1;
  • When I typed the title of the question there was no suggestion of similar answer, so I entered, always take care of it, thanks for the reference, delete my question?

1 answer

0

Following what you said, the query would be this way:

"UPDATE tblQtd SET qtdItens = qtdItens-1 WHERE idItenRef = ".$id;
  • Thanks for the help, it worked.

  • 1

    This does not appear to be a solution. Probably when changing the query there was a correction of another syntax problem - What would be the problem in the original query that was fixed here?

  • @Bacco has removed ( .... ) from the query and the bass accents. There is still room for improvement?

  • 1

    @Eliseub. the point is that it only changed the style of code, and still removed the security of LIMIT, but apparently did not change anything that effectively alters the behavior of the query. My biggest doubt here is whether there was any benefit and what really changed. As you commented that "worked" and the other query should also have worked, I found strange the purpose of the post and the proposed changes. Remembering that even unnecessary parentheses and backticks, anyway this does not interfere in the execution of the query.

  • @Bacco I kept the LIMIT by prevention, I had run problems in previous applications and I learned from the mistake and it was time burned, and I keep the accent severe by avoiding conflicts, but I tested mine and his and both worked out, for lack of a detailed answer I stayed with her, but it did not take 'reply accepted'.

Browser other questions tagged

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