title: Complex arithmetic in MySQL query
body: <p>I have a table as follows:</p>
  
  <pre><code>product | quantity | price | gift | giftprice
  --------|----------|-------|------|----------
  1       | 2        | 9.99  | 0    | 4.99
  2       | 3        | 3.50  | 1    | 2.25
  3       | 1        | 4.75  | 1    | 1.50
  </code></pre>
  
  <p>What I'd like to have an SQL query that will give me a figure that gives me the sum of all the records with quantity multiplied by price with the giftprice being added to the price before multiplication only if the 'gift' field is set to 1.</p>
  
  <p>Pseudocode</p>
  
  <pre><code>foreach(record){
     if(gift == 1){ linetotal = (price + giftprice) * quantity; }
     else { linetotal = price * quantity; }
     total = total + linetotal;
  }
  </code></pre>