0

I am getting sum of all cells in column with following query..

$result = mysql_query("select SUM(previous_balance) as balance from table_name ");
 $row = mysql_fetch_assoc($result);   
 $item_number = $row['balance'];
 $total1 = $item_number ;

 echo $total1; 

But I want to Omit last cell value in SUM. How can I do it ?

darshan
  • 319
  • 3
  • 16
  • 1
    Without an ORDER BY clause, referring to the "last" row doesn't really make sense. – Alex Howansky Jan 20 '15 at 19:51
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 20 '15 at 19:53
  • select the last value in a new query in the same transaction and deduct that value. Or use UNION to get both values in one query? Or create a stored procedure. Many options available. – oldwizard Jan 20 '15 at 19:55
  • Can you share some sample data and the result you want to get? – Mureinik Jan 20 '15 at 19:56
  • @pcguru ur suggestion "select the last value in a new query in the same transaction and deduct that value" worked for me...thnx – darshan Jan 20 '15 at 20:00
  • @darshan: have your table any date-time fields? – Ruben Kazumov Jan 20 '15 at 20:03

1 Answers1

0

According to this answer (if I'm reading it right), the following should work to select all except for the last row, and then to use that to sum over all items

SELECT SUM(previous_balance) as balance FROM table_name WHERE my_primary_key <> (SELECT MAX(my_primary_key) FROM table_name);
Community
  • 1
  • 1
Alec Deitloff
  • 447
  • 4
  • 12