php - Laravel/MySQL 在 where 子句中使用计算别名列

标签 php mysql laravel orm eloquent

有人可以帮忙编写下面的代码吗?由于列“距离”不存在,即使我将其定义为...,我也收到错误...

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;    

}

最佳答案

HAVING 可用于比较别名的值。

having('distance', '<', $distance);

关于php - Laravel/MySQL 在 where 子句中使用计算别名列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874964/

相关文章:

php - 两个范围之间的MySQL查询

mysql - 如果行 > 0 则更新或在 Mysql 上插入

mysql - 在一个查询中从两个表中选择多个 COUNT 的子查询

php - Laravel - 限制特定 API 路由的速率

laravel - 在 CentOS 上安装 Laravel 5

php - MySQL数据库设计: One user table, 两个用户组?

php - php 中的登录状态更新

php - 重新排列存储在变量中的日期格式

php - 自定义 Artisan 命令以执行多个命令

php - 如何从 Stacey App 中的逗号分隔列表值创建链接标签?