Jump to content
xisto Community
krap

Mysql Update Everything Bit confusing

Recommended Posts

i need to make a script that will take a number from every record in a mysql table, takeaway 1 from them all then replace them all in the right places. so if it looked like

Record 1 5

Record 2 8

Record 3 9

then i exectue the script then it turns into:

Record 1 4

Record 2 7

Record 3 8

i cant think of a way to do this so if you can help id be very greatful.

 

Thanks in advance

Andre

Share this post


Link to post
Share on other sites

I'm not sure if this will work, and there is probably a better way to do this but you might as well give it a try.

ID is the primary key of the table.
C1 is the column with the number to be subtracted.
C2 is the number you are subjection from.
T1 is the name of the table.

<?php$result = mysql_query("select ID, C1, C2 from T1");while($row = mysql_fetch_array($result)){  $row[2] = $row[2] - $row[1];  mysql_query("update T1 set C2 = '$row[2]' where ID = '$row[0]");}?>

Let me know if I screwed up and what the error is and I'll see if I can fix it....

Share this post


Link to post
Share on other sites

Uhh.. That's just a simple query like beeseven posted..

mysql_query("UPDATE tablename SET columnname=columnname - 1") or die(mysql_error());

It would just update the column as the column - 1.

Share this post


Link to post
Share on other sites

Yep, beeseven's and mike's querys should work, it only becomes tricky if the field is some sort of text, in which case you'd be forced to pull all data, convert the text to numbers, subtract one, and then store it (which would autoconvert it back to text).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.