有没有人可以帮我写下面的代码。我收到一个错误,因为列‘距离’不存在,即使我已经定义为...
public static function getByDistance($lat, $lng, $distance)
{
$result = Auction::join('users', 'auctions.user_id', '=', 'users.id')
->select(DB::raw('users.id', '( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) as distance'))
->where ('distance', '<', $distance)
->get();
return $result;
}发布于 2018-08-16 18:41:29
MySQL在一些版本之前就去掉了这种与别名列的比较。
它只适用于排序、分组和拥有。
您可以使用:
whereRaw( '(SUBQUERY) < ?', ['distance' => $distance])我建议对空值使用coalesce。
编辑
提供的另一个答案也是有效的。
发布于 2018-08-16 19:16:26
HAVING可用于比较别名的值。
having('distance', '<', $distance);https://stackoverflow.com/questions/51874964
复制相似问题