Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Mysql Update Everything, Bit confusing
krap
post Apr 8 2005, 04:20 PM
Post #1


Super Member
*********

Group: Members
Posts: 204
Joined: 6-October 04
From: London, uk
Member No.: 1,444



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
Go to the top of the page
 
+Quote Post
Argoth
post Apr 8 2005, 08:56 PM
Post #2


Newbie [Level 2]
**

Group: Members
Posts: 29
Joined: 6-April 05
Member No.: 5,318



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.

CODE

<?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....
Go to the top of the page
 
+Quote Post
beeseven
post Apr 10 2005, 12:55 AM
Post #3


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



If the column is set as a number (INT, FLOAT, DOUBLE, etc.), then I think you can just do this:
CODE
mysql_query("UPDATE `tablename` SET `colname`=`colname`-1");
Go to the top of the page
 
+Quote Post
Mike
post Apr 10 2005, 01:52 PM
Post #4


Owner of Sub-Zero
********

Group: Members
Posts: 159
Joined: 17-November 04
Member No.: 2,325



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

CODE

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


It would just update the column as the column - 1.
Go to the top of the page
 
+Quote Post
bjrn
post Apr 14 2005, 10:24 PM
Post #5


Super Member
*********

Group: Members
Posts: 378
Joined: 8-January 05
Member No.: 3,174



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).

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Problem On Mysql "order By"(5)
  2. Php News Script(19)
  3. Increment A Mysql Column(7)
  4. Can You Add Images Into A Mysql Database?(20)
  5. Subquery In Mysql(5)
  6. Creating Profiles In Php/mysql ?(7)
  7. Php Search Engine Script For Mysql Database(11)
  8. Some Mysql Basics(4)
  9. Php, Mysql, Apache(4)
  10. Php And Mysql Applications(3)
  11. Php Update(4)
  12. Getting Php 5 To Work With Mysql(0)
  13. Problem With A Mysql Join(2)
  14. The Artists Tutorials :mysql Basic Commands(0)
  15. [mysql]get Id Of Loged In User?(7)
  1. [mysql/php]need Som Basic Help(13)
  2. [php/mysql]id Trouble [resolved](3)
  3. Mysql Won't Update(4)
  4. Php + Mysql Question!(4)
  5. Tools Needed!(9)
  6. Best Sites For Learning Php-mysql(4)
  7. Php And Mysql Programming(2)
  8. Best Php And Mysql Editor For Noobs(6)
  9. Html Form!(4)
  10. Concurrency Problem?(1)
  11. Sending E-mails After News Update?(8)
  12. Mysql Error(3)
  13. Create Table - Mysql Code - Help(1)


 



- Lo-Fi Version Time is now: 6th October 2008 - 11:26 AM