How to implement HAVING clause with AND KEYWORD in MYSQL Query
<p>I am making an application where Item will be delivered from one place to another. The database saves the sending location or address (along with the sending latitude and longitude) and the receiving address or location(along with the destination latitude and longitude).</p>

<p>I am making a search application which will receive data like </p>

<ol>
<li>sending_latitude $Flat</li>
<li>Sending_longitude $Flon</li>
<li>Destination_latitude $Tlat</li>
<li>Destination_longitude $Tlon</li>
<li>From_date $date</li>
<li>Radius $r</li>
</ol>

<p>Then it will search the item list to find the locations of the points within the specified radius parameter of the given geographic position and having the date matching <code>from_date</code>.</p>

<h2>My Query:</h2>

<pre><code>SELECT *, (
            6371 *
            acos(
                cos( radians( $Flat ) ) *
                cos( radians( `sending_from_latitude` ) ) *
                cos(
                    radians( `sending_from_longitude` ) - radians( $Flon )
                ) +
                sin(radians($Flat)) *
                sin(radians(`sending_from_latitude`))
            )
        ) `senddistance`, (
            6371 *
            acos(
                cos( radians( $Tlat ) ) *
                cos( radians( `sending_to_latitude` ) ) *
                cos(
                    radians( `sending_to_longitude` ) - radians( $Tlon )
                ) +
                sin(radians($Tlat)) *
                sin(radians(`sending_to_latitude`))
            )
        ) `todistance`
FROM `items` as Item
HAVING (`senddistance` &lt; $rad AND `todistance` &lt; $rad)
ORDER BY `senddistance`
</code></pre>

<p>It's working fine as anticipated. However, I want to add another conditing <code>AND from_date = $date</code> in the Conditions.</p>

<p>So I wrote the query like this:</p>

<h2>Modified Query:</h2>

<pre><code>SELECT *, (
            6371 *
            acos(
                cos( radians( $Flat ) ) *
                cos( radians( `sending_from_latitude` ) ) *
                cos(
                    radians( `sending_from_longitude` ) - radians( $Flon )
                ) +
                sin(radians($Flat)) *
                sin(radians(`sending_from_latitude`))
            )
        ) `senddistance`, (
            6371 *
            acos(
                cos( radians( $Tlat ) ) *
                cos( radians( `sending_to_latitude` ) ) *
                cos(
                    radians( `sending_to_longitude` ) - radians( $Tlon )
                ) +
                sin(radians($Tlat)) *
                sin(radians(`sending_to_latitude`))
            )
        ) `todistance`
FROM `items` as Item
WHERE from_date = $date
HAVING (`senddistance` &lt; $rad AND `todistance` &lt; $rad)
ORDER BY `senddistance`
</code></pre>

<p>It just simply returns empty result.</p>

<p>What am I doing wrong?</p>