Good day to all. I have a table of users with fields:

id +-------------+ percent +-------------+ sum +-------------+ filter 

I need for each user with filter = 1 to increase the value in the sum field by a percentage (from the percent field) of the specified number. That is, if the specified number = 100 (may vary), then the user with percent = 15 needs to increase the value of the sum field by 15 (15 percent of 100).

All that I can now is to select all users with filter = 1, and then in a cycle using the programming language, read the percentage for each user and make a request in the database to set the new value of the sum field ... It seems to me that this is not The best solution and the task can be done using sql.

I would be grateful if someone tells you which way to look or give an example sql query for a given table.

  • And then you do not need to change the value of the filter? - smackmychi 2:51 pm
  • No, filter is unchanged. - zidim
  • @zidim; If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

2 answers 2

 UPDATE `table` SET `sum`=`sum`*(1+percent/100) WHERE filter=1 
  • So I don't even agree on a calculator, and attempts to modify it for myself lead to errors. I have each user their own percent and their sum. So, I need to find out how many units make up the user percent of a given number (in example 100) and add these units to the sum value. I read the documentation, I tried to correct it myself in the console. Correct, please, who understands how this is done. - zidim
  • is the percentage taken from the sum field or from where? - Ale_x
  • @Ale_x, Мне нужно для каждого пользователя с filter=1 увеличить значение в поле sum на процент (из поля percent) от заданого числа. То есть, если заданное число = 100 (может изменяться), то пользователю с percent=15 нужно увеличить значение поля sum на 15 (15 процентов от 100). Мне нужно для каждого пользователя с filter=1 увеличить значение в поле sum на процент (из поля percent) от заданого числа. То есть, если заданное число = 100 (может изменяться), то пользователю с percent=15 нужно увеличить значение поля sum на 15 (15 процентов от 100). - smackmychi
  • I would teach Russian, let them teach me))) Why a question that you cannot technically formulate, still piled up with complex and long sentences (('if the given number = 100 (can change)' - let the author explain in more detail what does it mean to change. Is there a number for each line? Or have you now performed a query with the number 100, and after tomorrow with the number 200? - Ale_x
  • today with the number 100, and in a minute with the number 200 it is transferred to the backend from the frontend. @smackmychi solution suits me, thank you very much - zidim
 update A set sum = sum + (<число>/100*percent) where filter=1;