title: MYSQL AND query to satisfy on same column
body: <p><strong>Project Aim :</strong></p>
  
  <p>We are developing bus timing <code>Api</code> where user will search for buses.</p>
  
  <p>Following are my <code>table structure</code></p>
  
  <p>I have following <code>tables</code></p>
  
  <p><strong>buses</strong>  </p>
  
  <pre><code>id | bus_name
  </code></pre>
  
  <p><strong>Description of table:</strong>  Store all buses Names</p>
  
  <p><strong>routes</strong> </p>
  
  <pre><code>id | route_name
  </code></pre>
  
  <p><strong>Description of table:</strong>  Store All city names</p>
  
  <p><strong>stops</strong> </p>
  
  <pre><code>id | stop_name
  </code></pre>
  
  <p><strong>Description of table:</strong>  All stop names</p>
  
  <p><strong>stop_orders</strong> </p>
  
  <pre><code>id | route_id | stop_id | stop_order
  </code></pre>
  
  <p><strong>Description of table:</strong> here i will assign stops for city and stop_order column help to identify which stop next to each other</p>
  
  <p><strong>bus_timing</strong></p>
  
  <pre><code>id | stop_order_id | bus_id | bus_timing | trip | trip_direction
  </code></pre>
  
  <p><strong>Description of table:</strong> Here i will assign buses for route stops along with time and trip and direction</p>
  
  <p><strong>Output Expecting:</strong></p>
  
  <ol>
  <li><p>When user search between source to destination with time then Api must return all buses list with time</p></li>
  <li><p>if direct buses not there then interconnected buses should show</p></li>
  </ol>
  
  <p>For example if user search between <code>stop_8</code> to <code>stop_18</code> with <code>01:00:00</code> to <code>12:00:00</code> then all buses list with time should show.if direct buses not there to travel between two stops then interconnected link buses list should show</p>
  
  <p>Output what i got is</p>
  
  <p><a href="https://stackoverflow.com/questions/48231623/php-compare-associative-array-based-on-condition">PHP compare associative array based on condition</a></p>
  
  <p><strong>Present return result issue is</strong></p>
  
  <ol>
  <li><p>It will return all buses even though if bus is only travel to <code>stop_8</code> but not <code>stop_18</code>.But my result must return only those buses which will travel between two stops i mean it must fall between both stops .</p></li>
  <li><p>Even i have no idea how to find interconnected buses list</p></li>
  <li><p>When time range is long then there is chance of same bus will travel(trip and direction) multiple times </p></li>
  </ol>
  
  <p><strong>Updates</strong>
  Still looking for answer .Right now given answer has some points so offered bounty</p>