MySQL的每周价格

时间:2013-12-30 08:31:47

标签: php mysql sql

我有以下表结构

id, startdate, hotelid, price

在前端我显示最低和最高价格为$35 - $671 (per day)这是酒店每天的最低和最高价格。我可以根据GROUP BY hotelid获取每日数据,并获得最低/最高价格,如下所示:

SELECT
    hotelid,
    MIN(price) AS `minprice`,
    MAX(price) AS `maxprice`
FROM
    hotel_price
WHERE
    startdate > NOW()
    AND
    price > 0
GROUP BY
    hotelid;

现在,客户希望将每周价格显示为最小/最大价格。但我感到困惑,因为我在表格中每天都有数据,我应该如何计算并显示每周价格最小值为最小值?我们如何在mysql查询中执行此操作?

2 个答案:

答案 0 :(得分:1)

尝试在哪里

WHERE price > 0 and startdate BETWEEN NOW() and DATE_SUB(NOW(), INTERVAL 7 DAY)

答案 1 :(得分:1)

您可以尝试WEEK()YEAR()功能,如下所示:

SELECT
    hotelid,
    week(startdate) AS `week`,
    year(startdate) AS `year`,
    min(price) AS `minprice`,
    max(price) AS `maxprice`
FROM
    hotel_price
WHERE
    startdate > now()
    AND
    price > 0
GROUP BY
    hotelid,  year(startdate), week(startdate);