0

I have table like this

   A   
======
   2
   7
   7
   8
   9
   3
  ...
   1
======

I need to sum all value of row in db, except last value (that is '1'). I know how to sum all value, my question how to sum all value except last value?

$sum = 0;
$a = mysql_query('SELECT A FROM tbl');
while($row = mysql_fetch_array($a))
{
   $jpos = $row['A'];
   $sum     += intval($jpos);
}
echo $sum;

thanks for your response.

5
  • does the last row has highest Id ? Commented Sep 30, 2013 at 0:13
  • 1
    SQL servers don't guarantee an order unless you specify with ORDER By. How do you know which should be the last line? Commented Sep 30, 2013 at 0:15
  • deepak and mike, sory for the mistake, i missed to update the question. i have update it. thanks Commented Sep 30, 2013 at 0:18
  • @HerlambangPermadi the update doesnt answer my question.. Commented Sep 30, 2013 at 0:22
  • 2
    Why don't you let MySQL compute the sum for you? I haven't tested it, but something like this should work: SELECT SUM(A) FROM tbl WHERE id < (SELECT MAX(id) FROM tbl) Commented Sep 30, 2013 at 0:24

2 Answers 2

2

I'm going to make an assumption that the table (tbl) in question will have at least two columns labelled:

  1. id
  2. value

This can be done in mysql:

SELECT SUM(value)
FROM tbl
WHERE id <> (SELECT MAX(id) FROM tbl)
Sign up to request clarification or add additional context in comments.

Comments

1
$sum = 0;
$last = 0;
$a = mysql_query('SELECT positif FROM data_aali where id_perusahaan=1');
while($row = mysql_fetch_array($a))
{
   $last = intval($row['positif']);
   $sum += $last;
}
$sum -= $last;
echo $sum;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.