I have a Postgresql table named stores which contains a store's location (lat, long), and I can find near stores from a store using query. However, I can't find a query to create a 'ready'-generated table which create list of near stores for every store. This is the query I used to get list of near stores:
select mds.id, mds.store_name
from public.store mds,
(select latitude, longitude from public.store where id = '3f6077c0-c56b-4570-883f-4c16dc19855e') as st,
sqrt(111.12 * (mds.latitude - st.latitude) * 111.12 * (mds.latitude - st.latitude) + (111.12 * (mds.longitude - st.longitude) * cos(st.latitude / 92.215)) * (111.12 * (mds.longitude - st.longitude) * cos(st.latitude / 92.215))) as distance
where distance <= 20
order by distance
limit 100
I can't replace the '3f6077c0-c56b-4570-883f-4c16dc19855e' by public.store.id. The table columns of store table is:
| id | store_name | latitude | longitude |
Please help me with this request. Thank you very much.