-1

我正在努力完成这项工作,这个查询应该给出两个日期之间最便宜的价格,但不知何故它总是给出最便宜的报价,它有点忽略日期

SELECT offer_id 
FROM travel_offers o
WHERE price = (
    SELECT MIN(price) as minprice 
    FROM travel_offers om 
    WHERE 
        o.latitude = om.latitude AND
        o.longitude = om.longitude AND
        (o.time_from BETWEEN '2012-07-01' AND '2012-11-31') 
        AND
        (o.time_to BETWEEN '2012-07-01' AND '2012-11-31')
)
4

3 回答 3

0

如果经度和纬度很重要,试试这个:

SELECT o.longitude, o.latitude, offer_id 
FROM travel_offers o
WHERE o.time_from BETWEEN '2012-07-01' AND '2012-11-31'
      AND o.time_to BETWEEN '2012-07-01' AND '2012-11-31')
      AND price = (
     SELECT MIN(price) as minprice 
     FROM travel_offers om 
     WHERE 
        o.latitude = om.latitude AND
        o.longitude = om.longitude AND
        (om.time_from BETWEEN '2012-07-01' AND '2012-11-31') 
        AND
        (om.time_to BETWEEN '2012-07-01' AND '2012-11-31')
)

如果经度和纬度无关紧要:

SELECT offer_id 
FROM travel_offers o
WHERE o.time_from BETWEEN '2012-07-01' AND '2012-11-31'
      AND o.time_to BETWEEN '2012-07-01' AND '2012-11-31')
      AND price = (
     SELECT MIN(price) as minprice 
     FROM travel_offers om 
     WHERE 
        (om.time_from BETWEEN '2012-07-01' AND '2012-11-31') 
        AND
        (om.time_to BETWEEN '2012-07-01' AND '2012-11-31')
)
于 2012-09-18T14:16:04.737 回答
0

不知何故,它总是提供最便宜的报价,而且它有点忽略了日期

我猜你1time_from应该是2012-07-01而且time_to应该是2012-11-31
因此,您需要像这样更正WHERE子句中1time_from的条件1time_to

WHERE 
    o.latitude = om.latitude AND
    o.longitude = om.longitude AND
    (o.time_from >= '2012-07-01') 
    AND
    (o.time_to <= '2012-11-31')
于 2012-09-18T14:16:15.270 回答
0
SELECT offer_id, MIN(price) as minprice 
FROM travel_offers
WHERE 
    time_from BETWEEN '2012-07-01' AND '2012-11-31'
    AND
    time_to BETWEEN '2012-07-01' AND '2012-11-31'
    and 
    time_from <= time_to
group by offer_id
order by minprice
limit 1
于 2012-09-18T14:12:15.750 回答