title: PHP MySQL remove quantity
body: <p>i met with situation when need to import values with quantity that will be spended many times by some qty.</p>
  
  <p>Eg. i have table with product material</p>
  
  <pre><code>ID    TYPE    QUANTITY    SPENT
  1     1       10           0
  2     1       100          0
  </code></pre>
  
  <p>Now i spent 20 qty of material ONCE and need to update field SPENT to be like this</p>
  
  <pre><code>ID    TYPE    QUANTITY    SPENT
  1     1       10           10     &lt;--- 0 left
  2     1       100          10     &lt;--- 90 left
  </code></pre>
  
  <p>Or i spend 8 qty first then another 20 qty, so table needs to be like this </p>
  
  <p>First - spent 8</p>
  
  <pre><code>ID    TYPE    QUANTITY    SPENT
  1     1       10           8       &lt;--- 2 left
  2     1       100          0       &lt;--- 100 left
  </code></pre>
  
  <p>Second - spent 20</p>
  
  <pre><code>ID    TYPE    QUANTITY    SPENT
  1     1       10           10       &lt;--- 0 left
  2     1       100          18       &lt;--- 72 left
  </code></pre>
  
  <p>So i need to separate spended material quantity by avaible and reduce till limit then reduce other one with avaible qty. </p>
  
  <p>What is the best way for this with php?</p>
  
  <p>Please any help will be appreciated, thank you!</p>