认为您想要的数量可能最好通过使用子选择来获得。
我假设 accomodation_rooms 是一个表,其中存储了一些住宿的多行,说明该位置有多少张床(即 1 x 5 床房间、2 x 3 床、5 x 2 床)。
SELECT
accomodation.*,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_url_en AS country_url,
accomodation_rooms.total_persons
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)
< '50'
INNER JOIN (SELECT accomodation_id, SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons FROM accomodation_rooms GROUP BY accomodation_id) accomodation_rooms
ON accomodation_rooms.accomodation_id = accomodation.id
AND accomodation_rooms.total_persons >= 1
WHERE db_countries.title_url_en LIKE '%country_title%'
AND accomodation.region_id = '8'
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
作为一个小问题,不确定您是否需要将原始选择作为子选择(或有一个 order by 子句,但您似乎没有限制,所以我认为它不会有任何影响) :-
SELECT
accomodation.id,
accomodation.aid,
accomodation.title_en,
accomodation.title_url_en,
accomodation.address,
accomodation.city_id,
accomodation.zip,
accomodation.district_id,
accomodation.province_id,
accomodation.region_id,
accomodation.country_id,
accomodation.mountain_id,
accomodation.stars,
accomodation.latitude,
accomodation.longitude,
accomodation.picture,
accomodation.valid_from,
accomodation.valid_to,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_url_en AS country_url,
accomodation_rooms.total_persons
FROM 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)
< '50'
INNER JOIN (SELECT accomodation_id, SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons FROM accomodation_rooms GROUP BY accomodation_id) accomodation_rooms
ON accomodation_rooms.accomodation_id = accomodation.id
AND accomodation_rooms.total_persons >= 1
WHERE db_countries.title_url_en LIKE '%country_title%'
AND accomodation.region_id = '8'
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
编辑 - 修改以获取每个援助的最新住宿记录,并加入该记录以获取最新住宿记录的其余部分。
SELECT
accomodation.*,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_url_en AS country_url,
accomodation_rooms.total_persons
FROM (SELECT aid, MAX(info_date_add) AS max_info_date_add FROM accomodation GROUP BY aid) accomodation_max
INNER JOIN accomodation ON accomodation_max.aid = accomodation.aid AND accomodation_max.max_info_date_add = accomodation.info_date_add
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)
< '50'
INNER JOIN (SELECT accomodation_id, SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons FROM accomodation_rooms GROUP BY accomodation_id) accomodation_rooms
ON accomodation_rooms.accomodation_id = accomodation.id AND accomodation_rooms.total_persons >= 1
WHERE db_countries.title_url_en LIKE '%country_title%'
AND accomodation.region_id = '8'
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
编辑 - 使用 GROUP BY 子句为距离添加 MIN。但是不确定这会比使用 DISTINCT 快得多。它会强制对 JOIN 进行大量计算(即,如果您有 100 条住宿记录和 100 条skiresort_locations 记录,那么这将导致 10000 次合理的复杂计算来确定距离。如果您可以在计算,那么这将节省相当多的时间(例如,去规范化一点,但也许你有一个区域表连接到 region_id 上的住宿,它可能包含该区域的最小和最大纬度和经度,你可以使用对于连接,然后将复杂的计算放入 WHERE 子句中)。还有一个用于 db_counties 的 LIKE 子句。
SELECT
accomodation.*,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_url_en AS country_url,
accomodation_rooms.total_persons,
MIN(((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)
FROM (SELECT aid, MAX(info_date_add) AS max_info_date_add FROM accomodation GROUP BY aid) accomodation_max
INNER JOIN accomodation ON accomodation_max.aid = accomodation.aid AND accomodation_max.max_info_date_add = accomodation.info_date_add
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)
< '50'
INNER JOIN (SELECT accomodation_id, SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons FROM accomodation_rooms GROUP BY accomodation_id) accomodation_rooms
ON accomodation_rooms.accomodation_id = accomodation.id AND accomodation_rooms.total_persons >= 1
WHERE db_countries.title_url_en LIKE '%country_title%'
AND accomodation.region_id = '8'
GROUP BY accomodation.id,
db_cities.title_en,
db_cities.title_url,
db_countries.title_url,
accomodation_rooms.total_persons
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