0

我无法弄清楚这种类型的查询:这是我的查询:

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

我希望有人能理解我的艰难解释(对不起我的英语)并会帮助我......

4

2 回答 2

1

谢谢,我设法正确地工作了:)

对于需要它的人来说,这就是我的选择的外观:

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, latitude, longitude, 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 
JOIN skiresort_locations 
  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) 
        < '20' 
WHERE 
  db_countries.title_url_en LIKE '%country_name%' AND 
  accomodation.region_id = '8' 
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
于 2013-06-20T05:26:49.560 回答
0

连接可以在任意表达式上完成,而不仅仅是field1=field2-type 表达式。

JOIN othertable ON distance_function() < 50

完全可以接受。重要的是表达式是否可以评估为真/假状态。如果为真,则正在考虑的记录将成为联接的一部分。如果为假,则排除记录。你如何得到那个真/假并不重要,只是你最终得到的是真/假。

于 2013-06-19T14:16:02.887 回答