Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update MySQL table with some math?

Tags:

php

mysql

I'm writing a php script which will update my sql filed with some math. Should I make a query to get the filed value first then update the filed or there is something to update the filed with in the query it self here is the math

$sum = 5 ;
$filed = $filed +(($filed * 100) / $sum) ;

This how the math goes with in the PHP. Is there any way to get this math done by the query it self ?

like image 405
Marco Avatar asked Jul 10 '11 02:07

Marco


People also ask

How do I UPDATE multiple values in MySQL?

MySQL UPDATE multiple columnsMySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

How do I UPDATE all values in a column in MySQL?

To set all values in a single column MySQL query, you can use UPDATE command. The syntax is as follows. update yourTableName set yourColumnName =yourValue; To understand the above syntax, let us create a table.


1 Answers

Assuming filed is the name of a column in your table, you can do the math right inside the MySQL query, without involving PHP:

UPDATE table SET filed = (filed + ((filed * 100) / 5) WHERE some_condition;

Just be sure to set the appropriate conditions in your WHERE clause so you don't update rows you don't intend to update.

like image 88
Michael Berkowski Avatar answered Sep 17 '22 15:09

Michael Berkowski