我遇到查詢問題。 我想做一個子查詢,但它不返回結果。Laravel子查詢whereIn
這裏是我的代碼:
$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));
的PostgreSQL相當於:
select id, city, 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) as distance
from representations
where id in (
select id
from representations
where 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
)
謝謝
你在你的主要select子句中有一個比較 – inarilo