View Full Version : UPDATE and Add at same time

01-31-2010, 03:32 PM
table abc

id name qty
1 banana 2
2 apple 7

now i want to update this table and set banana qty to 2(orignianl value)+3 (new value) in one single php mysql statement, how can i do it?

i do not want to fetch the banna record first , then add the value, then update again, like that is wasting queries and affecting performance

01-31-2010, 03:35 PM
Update abc set qty=qty+1 where id = ...

01-31-2010, 03:38 PM

01-31-2010, 04:34 PM
You can also set it up in variables. If you like want a user to submit how many bananas they have.

if (isset($_POST['submit']))
$bananas = $_POST['userbana'];
$apples = $_POST['apples'];

mysql_query("UPDATE users WHERE bananas=bananas+$bananas, apples=apples+$apples WHERE id='$myid'");