tags:

views:

78

answers:

2

Hello, I wanted to know whether i can direct update mysql datas to add (+) the values, without needing to fetch them.

For example:

My database is like this, data1 = 56 so to add a 4 to it, i will first fetch the data from the database then,

$data1 = $data1 + 4; and finally again perform a Update query.

So is there is way i can send like add +4 to the current value of data1.

I am using php lanuage.

Thank You

+8  A: 

Like... this?

UPDATE
  MyTable
SET
  Data = Data + 4
WHERE
  Myid = 123

A look into basic SQL syntax seems advisable. ;-)

If you are working with PHP to manipulate your data, a deeper look into the mysqli_* functions family in general and prepared/parameterized statements in particular is advisable as well.

Tomalak
D'oh - pipped me!
Dominic Rodger
sorry for once i thought this kind of thing dont exists, so i didnt looked in documentation.
Shishant
+2  A: 
mysql_query("UPDATE `mytable` SET `data1` = `data1` + 4");

You'll want a WHERE clause unless you want to update every row.

Dominic Rodger