Jump to content

[SOLVED] Help with query


dreamwest

Recommended Posts

I need to recalculate a whole column based on another columns row change

 

Database structure:

 

id | cost | total

 

1 | 20    | 20

2 | 40    | 60

3 | 10    | 70

4 | 70    | 140

5 | 60    | 200

 

etc...

 

Say i use a form to edit a value from cost like this (in bold)

 

id | cost | total

 

1 | 20    | 20

2 | 40    | 60

3 | 20   | 70  <-----New value from 10 to 20 in cost

4 | 70    | 140

5 | 60    | 200

 

etc...

 

How can i recalculate the rest of the total column so it ends up like this:

 

 

id | cost | total

 

1 | 20    | 20

2 | 40    | 60

3 | 20    | 80 

4 | 70    | 150

5 | 60    | 210

 

 

Link to comment
Share on other sites

I don't feel there's any need to have that column to be honest.

 

Let's say you pull the data resulting in row 4 getting pulled.

 

We have ID of 4 and COST of 70.

 

We can run a query to total up everything previously.

 

SELECT SUM(cost) AS total FROM table WHERE id<=4

Link to comment
Share on other sites

I don't feel there's any need to have that column to be honest.

 

Let's say you pull the data resulting in row 4 getting pulled.

 

We have ID of 4 and COST of 70.

 

We can run a query to total up everything previously.

 

SELECT SUM(cost) AS total FROM table WHERE id<=4

 

Your right, Thanks.

 

I didnt think of doing it this way, this will get rid of half my code

Link to comment
Share on other sites

Ok i rewrote the query to get the total of cost, but this is my first time using sum() and it doesnt seem to be working:

 

$result = mysql_query("SELECT SUM(cost) AS total FROM table ") or die(mysql_error());  

$row = mysql_fetch_array( $result );
$total =   number_format($row['SUM(cost)']/100,2);

echo $total;

Link to comment
Share on other sites

Ok i rewrote the query to get the total of cost, but this is my first time using sum() and it doesnt seem to be working:

 

$result = mysql_query("SELECT SUM(cost) AS total FROM table ") or die(mysql_error());  

$row = mysql_fetch_array( $result );
$total =   number_format($row['SUM(cost)']/100,2);

echo $total;

 

edit this:

$total = number_format($row['total']/100,2);

//because you have set sum(cost) as total..

Link to comment
Share on other sites

I wouldn't just "copy & paste" the query I posted into your code and expect it to work straight away.

 

I didn't know what your tables were called so I used "table" - this needs to be replaced with the name of your table.

 

$total = number_format(($row['total']/100),2);

 

Have added brackets to the value to make sure that gets done first (the division) before it gets passed to number_format()

Link to comment
Share on other sites

I wouldn't just "copy & paste" the query I posted into your code and expect it to work straight away.

 

I didn't know what your tables were called so I used "table" - this needs to be replaced with the name of your table.

 

$total = number_format(($row['total']/100),2);

 

Have added brackets to the value to make sure that gets done first (the division) before it gets passed to number_format()

 

Woops - i didnt notice the AS total. I figured it out

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.