我的查询有问题。 我想创建一个子查询,但它不会返回结果。
这是我的代码:
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("d:\home\site\wwwroot\bin\System.Web.Mvc.dll").FileVersion
PostgreSQL等价物:
$distance_representations = Representation::select('id')
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->limit(10)
->get();
$representations = Representation::select('id', 'city', DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->whereIn('id', $distance_representations)
->get();
return view('search', array('representations' => $representations));
谢谢
答案 0 :(得分:1)
也许你可以试试这个
$distance_representations = Representation::find('id')
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->limit(10)
->get();
$representations = Representation::all()
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->whereIn('id', $distance_representations.id)
->get();
希望这会对你有所帮助。
答案 1 :(得分:1)
我不明白为什么你需要子查询?除非我遗漏了某些内容,否则您应该能够使用单个查询选择数据。
@{ .... }
答案 2 :(得分:1)
我找到了解决方案。我无法做得更好。谢谢。
此解决方案工作!
$representations_with_distance = Representation::select('id', 'city', DB::raw('round(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))::numeric, 1) as distance'));
$representation = DB::table(DB::raw("({$representations_with_distance->toSql()}) as rd"))
->where('distance', '<', '?')
->orderBy('distance')
->offset(0)
->limit(10)
->setBindings([$latitude, $longitude, $latitude, $radius])
->get();
return view('search', array('representations' => $representation));