我无法弄清楚这种类型的查询:这是我的查询:
SELECT
accomodation.*,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_url_en AS country_url
FROM
(SELECT id, aid, title_en, title_url_en, address, city_id, zip, district_id, province_id, region_id, country_id, mountain_id, stars, picture, valid_from, valid_to FROM accomodation ORDER BY info_date_add DESC) AS accomodation
LEFT JOIN db_cities
ON db_cities.id = accomodation.city_id
JOIN db_countries
ON db_countries.id = accomodation.country_id
WHERE
db_countries.title_url_en LIKE '%country%'
AND db_countries.id = '202'
GROUP BY accomodation.aid
ORDER BY CASE
WHEN accomodation.valid_to>=NOW() AND accomodation.valid_from<=NOW() THEN 0
WHEN NOW()>accomodation.valid_to AND accomodation.valid_to!='0000-00-00' THEN 1
ELSE 2 END, accomodation.title_en
LIMIT 10 OFFSET 0
它工作得很好......我需要做的是再加入一个名为skiresort_locations的表,但问题是我不能使用ON,因为它们没有被任何列加入。我想通过从纬度和经度计算的距离加入它。
表skiresort_locations 具有纬度和经度列,表住宿也具有纬度和经度列。我想加入表skiresort_locations并显示住宿,距离不大于50公里。我有一个想法为 ON 进行子查询,但我不知道这是否可能。
这是计算距离的选择:
(((acos(sin((skiresort_locations.latitude*pi()/180))
* sin((accomodation.latitude*pi()/180))
+ cos((skiresort_locations.latitude*pi()/180)) * cos((accomodation.latitude*pi()/180))
* cos(((skiresort_locations.longitude - accomodation.longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) AS distance
我希望有人能理解我的艰难解释(对不起我的英语)并会帮助我......