代码:
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)
答案 0 :(得分:1)
paginate()
函数,执行两个查询:
第二个查询出现问题,其中count(*) as aggregate
是唯一选择的字段,没有像distance
这样的字段可以执行操作。
仅get()
函数起作用,因为它仅执行一个查询以获取具有给定where条件的所有记录。
解决方案:在where
中使用距离条件,而不是having
,因为它更有效。但是在这里,由于条件不适用于别名,因此需要重新编写相同的距离计算。代码应为:
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);
}
答案 1 :(得分:0)
使用Epic2RunnerTest.java
Laravel首次启动时
paginate
查询以获取记录数,因此,由于在此查询中您还拥有SELECT count(*)
,因此SQL显然不知道此查询中的having('distance', '<', '500')
是什么。解决方案是使用手动分页(更复杂的一种)或重复两次距离定义:
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
中第二次