title: MySQL php Linear Interpolation of Table
body: <p>I have a table titled "psytable_moist_air" shown below:
  <img src="https://i.stack.imgur.com/mkLSi.jpg" alt="enter image description here"></p>
  
  <p>I'm trying to develop a MySQL statement that will interpolate a value that may be between records.  <em>(EDIT: If it is easier to do the math in PHP I'm open for that solution too!]</em></p>
  
  <p><strong>Example:</strong> I would like to know the "hda" value where "T" = 17.8. </p>
  
  <p>Notice there is no record where "T"=17.8. However, because this table is linearly related, I can get the "hda" value where "T"=17 and the "hda" value where "T"=18 and do a linear interpolation of the values.</p>
  
  <p>The result math would look like this:<br/>
  (Get the "hda" value where T=17 : hda = 17.102)<br/>
  (Get the "hda" value where T=18 : hda = 18.108)<br/></p>
  
  <p><img src="https://i.stack.imgur.com/3q00G.jpg" alt="enter image description here"></p>
  
  <p><strong>EDIT</strong>:
  The only way I can think of is to do two MySQL statements to grab the smaller and larger values:</p>
  
  <pre><code>SELECT MAX(`T`), MAX(`hda`) FROM `psytable_moist_air` WHERE `T`&lt;17.8
  SELECT MIN(`T`), MIN(`hda`) FROM `psytable_moist_air` WHERE `T`&gt;17.8
  </code></pre>
  
  <p>Then I would use these two values to do the interpolation. This works but seems pretty inefficient. Can anyone come up with a better solution??</p>
  
  <p>If anyone has any advice it would be much appreciated!</p>
  
  <p>.</p>