日期之间的MySQL查询无法正常工作

时间:2012-09-18 14:04:05

标签: mysql

我正在努力使这项工作,这个查询应该给两个日期之间最便宜的价格,但不知何故,它总是提供最便宜的报价,它有点无视日期<​​/ p>

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')
)

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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')
)

答案 2 :(得分:0)

  

不知何故,它总是提供最便宜的报价,而且它有点无视日期<​​/ p>

我猜你的1time_from应该是2012-07-01time_to应该是2012-11-31
因此,您需要在WHERE和[{1}}的{​​{1}}条款中更正您的条件,如下所示:

1time_from