Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the difference between two # values where itemname = the same for one specific id
    primarykey
    data
    text
    <p>I have a table called "items"</p> <pre><code>id month year **itemname** distmoney 1 12 2012 chicken 20 2 12 2012 pork 15 3 11 2012 chicken 21 4 11 2012 pork 15 </code></pre> <p>I am trying to find the difference between the "distmoney" for the same itemname between two months FOR ONE SPECIFIC ITEM ONLY (by using WHERE id = $id)</p> <p>Example:</p> <p>ID 1, itemname chicken. month 12 distmoney is 20, while month 11 distmoney is 21. I want to be able to calculate the difference of 1 for id=1, itemname=chicken. It should ONLY display the difference for chicken, not any other item names (like pork).</p> <p>Right now I have the php code to calculate the difference between two numbers, but I am having a tough time figuring out how to grab the previous month distmoney. </p> <p><strong>Being able to find the previous month's distmoney for a specific itemname and inserting it into a new column called "oldmoney" would also work</strong>, but I am not sure to how to do this.</p> <p>Example: for id 3, itemname chicken. Current distmoney is 20, and the previous month (found in id = 1) is 20. Let's take 20 and insert it into a new column under id 3.</p> <pre><code>&lt;?php foreach($rows as $row): $number1 = htmlentities($row['distmoney']) endforeach; $number1 = $row['distmoney']; $number2 = ????????; // THIS NEEDS TO BE THE PREVIOUS MONTH DISTMONEY VALUE if ($number1 &lt;= $number2) { $difference = "(Price Lowered) Price difference of $"; $result = $number2 - $number1; $percent = round(100.0*($number2-$number1)/$number1); echo $difference; echo $result; echo $percent; echo "%"; } elseif ($number1 &gt; $number2) { $result = $number1 - $number2; $percent = round(100.0*($number2/$number1-1)); $addition = "(Price Higher) Price difference of $"; echo $addition; echo $result; echo $percent; echo "%"; } </code></pre> <p>?></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload