我用谷歌搜索但没有发现任何相关内容。我有一个像这样的MySQL表:
+++++++++++++++++++++++++++++++ | roomID | date | price | +++++++++++++++++++++++++++++++ | 1 | 2012-10-10 | 10 | | 1 | 2012-10-11 | 10 | | 1 | 2012-10-12 | 10 | | 1 | 2012-10-13 | 12 | | 2 | 2012-10-10 | 15 | | 2 | 2012-10-11 | 15 | | 2 | 2012-10-12 | 15 | | 2 | 2012-10-13 | 16 | | 2 | 2012-10-14 | 16 | | 2 | 2012-10-15 | 16 | +++++++++++++++++++++++++++++++
我需要根据价格和roomID得到期限:
++++++++++++++++++++++++++++++++++++++++++++ | roomID | from | till | price | ++++++++++++++++++++++++++++++++++++++++++++ | 1 | 2012-10-10 | 2012-10-12 | 10 | | 1 | 2012-10-13 | 2012-10-13 | 12 | | 2 | 2012-10-10 | 2012-10-12 | 15 | | 2 | 2012-10-13 | 2012-10-15 | 16 | ++++++++++++++++++++++++++++++++++++++++++++
谢谢!
答案 0 :(得分:6)
select roomid,
min(date) as from,
max(date) as till,
price
from periods
group by price
order by price
答案 1 :(得分:4)
您可以尝试使用以下查询:
SELECT roomid, MIN(date) AS `from`, MAX(date) AS `till`, price
FROM tableName
GROUP BY price
ORDER BY price