0

代码:

if ($request->going){
    if($request->lattitude && $request->longitude){
        $CurrentIplat = $request->lattitude;
        $CurrentIplon = $request->longitude;
        $ads1 = DB::table('ads')
        ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
        ->select(
            'ads.*','categories.category_name',
            DB::raw("( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) AS distance")
                )
        ->where('ads.domain',$this->domain)
        ->groupBy('ads.id')
        ->orderBy('distance') 
        ->having('distance','<','500')
        ->get();
            }
        }

提供适当的输出。

使用 paginate() 时

if ($request->going){
    if($request->lattitude && $request->longitude){
        $CurrentIplat = $request->lattitude;
        $CurrentIplon = $request->longitude;
        $ads1 = DB::table('ads')
        ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
        ->select(
            'ads.*','categories.category_name',
            DB::raw("( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) AS distance")
                )
        ->where('ads.domain',$this->domain)
        ->groupBy('ads.id')
        ->orderBy('distance') 
        ->having('distance','<','500')
        ->paginate(10);
            }
        }

它抛出

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from `ads` left join `categories` on `ads`.`category_id` = `categories`.`id` where `ads`.`domain` = nz group by `ads`.`id` having `distance` < 5000)
4

2 回答 2

1

paginate()Laravel 中的函数,执行两个查询:

  1. 首先,在您的情况下获取具有给定分页限制的记录,例如 10。
  2. 其次,根据给定的where条件获取记录总数。

问题是在第二个查询中count(*) as aggregate,唯一选择的字段是哪里,并且没有字段喜欢distance执行操作。

只有get()函数起作用,因为它只执行一个查询来获取具有给定 where 条件的所有记录。

解决方案:使用距离条件where代替,having因为它更有效。但是这里同样的距离计算需要再次写入 where 条件不适用于别名。代码应该是:

if ($request->lattitude && $request->longitude) {
    $CurrentIplat = $request->lattitude;
    $CurrentIplon = $request->longitude;
    $queryDistance = " ( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) ) ";
    $ads1 = DB::table('ads')
            ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
            ->select('ads.*', 'categories.category_name', DB::raw("$queryDistance AS distance"))
            ->whereRaw('ads.domain = ' . $this->domain . " AND $queryDistance < 500")
            ->groupBy('ads.id')
            ->orderBy('distance')
            ->paginate(10);
}
于 2018-07-24T13:01:54.213 回答
0

使用paginateLaravel 首次启动时

SELECT count(*)

查询以获取记录数,因此因为在此查询中您having('distance', '<', '500')显然也有 SQL 不知道distance此查询中的内容。这里的解决方案是使用手动分页(更复杂的一种)或重复距离定义两次:

$distance = "( 6371 * acos( cos( radians('{$CurrentIplat}') ) * cos( radians( ads.Latitude ) ) * cos( radians( ads.Longtitute ) - radians({$CurrentIplon}) ) + sin( radians({$CurrentIplat}) ) * sin( radians( ads.Latitude ) ) ) )";


if ($request->going){
    if($request->lattitude && $request->longitude){
        $CurrentIplat = $request->lattitude;
        $CurrentIplon = $request->longitude;
        $ads1 = DB::table('ads')
        ->leftJoin('categories', 'ads.category_id', '=', 'categories.id')
        ->select(
            'ads.*','categories.category_name',
            DB::raw($distance.' AS distance'))
        ->where('ads.domain',$this->domain)
        ->groupBy('ads.id')
        ->orderBy('distance') 
        ->having(DB::raw($distance),'<','500')
        ->paginate(10);
            }
        }

因此,如您所见,上面的distance列被提取到$distance变量中,然后它被使用了两次——第一次使用select,第二次使用having

于 2018-07-26T06:59:05.230 回答