title: Select until sum lenght is close to value of php variable
body: <p>I have a PHP variable <code>$total_km</code>, which holds the exact value for example: 360</p>
  
  <p>Then in my database I have a table where I'm storing routes and how long they are.</p>
  
  <pre><code>     Route     ¦    Length 
  -------------------------------- 
   Praha - Brno  ¦ 120
  </code></pre>
  
  <p>Is there a way to select random routes until the summed length of routes are +-10 or equal to the predefined exact value of the variable <code>$total_km</code>.</p>
  
  <p>This is my query:</p>
  
  <pre><code>SELECT tabCesty.odkud AS odkud, tabCesty.kam AS kam, tabCesty.km AS km, SUM(tabCesty.km) AS celkem_km 
  FROM tabCesty
  INNER JOIN tabUsers ON tabCesty.user_id=tabUsers.ID 
  AND tabCesty.user_id = $id_uzivatele 
  </code></pre>