我正在制作一个应用程序,其中Item将从一个地方交付到另一个地方。数据库保存发送位置或地址(连同发送纬度和经度)和接收地址或位置(以及目的地纬度和经度)。
我正在制作一个搜索应用程序,它将接收
等数据然后,它将搜索项目列表,以查找给定地理位置的指定半径参数内的点的位置,并使日期与from_date
匹配。
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` < $rad AND `todistance` < $rad)
ORDER BY `senddistance`
按预期工作正常。但是,我想在条件中添加另一个条件AND from_date = $date
。
所以我写了这样的查询:
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` < $rad AND `todistance` < $rad)
ORDER BY `senddistance`
它只是简单地返回空结果。
我做错了什么?
答案 0 :(得分:0)
您是否尝试将HAVING
替换为WHERE
?
HAVING
后,GROUP BY
通常用于过滤
WHERE
在分组前过滤结果答案 1 :(得分:0)
如果条件是查询,则必须添加到何处 你不需要围绕这种情况的条件
的()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` < $rad AND `todistance` < $rad
ORDER BY `senddistance`
条件是在分组后过滤结果,然后你可以在having子句中添加条件,但是你不需要()
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` < $rad
AND `todistance` < $rad
AND from_date = $date
ORDER BY `senddistance`
无论如何做你没有使用聚合功能的事实,你没有团体,那么你不应该使用但只有在哪里
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
AND `senddistance` < $rad
AND `todistance` < $rad
ORDER BY `senddistance`