Hello Friends I'm trying to build a PHP function that gets the values of the first and second rows of a table and subtract them. Then the function gets the second and third values and subtract them, then the third and forth and so on until the end of the table. (The table will be sorted ASC and the value of the subtraction will be use for posterior use) Is this possible? Thanks
I think this is what you trying to do $sql="SELECT value FROM table"; $query=mysql_query($sql); $numRows=mysql_num_rows($query); $count=0; while($r=mysql_fetch_array($query)){ //build an array with the values $array[$count]=$r['value']; $count++; }// end while for($i=1; $i<$numRows; $i++){ $totalArray[$i-1]=$array[$i]-$array[$i-1]; //subtract the rows }//end for PHP: $totalArray as the results of the subtractions
if your going to do that in sql your are referring to stored procedures.. though theres a way in querying but it will be slower than a regular query.. use php for the computation as what Narrator posted.