对不起,我不能更具体,但我不知道如何在标题中描述我的问题..分别我不知道如何搜索类似的问题。
所以这是交易。我有这张记录表:
如您所见,我有在不同月份以不同类型输入的数据。我的目标是让最后输入的金额按类型分组,这应该受日期限制。
例如:
If I specify a date 2011-03-01, the query should return 3 records with amount - 10, 20 and 30, because for type=1 the nearest inserted record to '2011-03-01' is on date 2011-01-01 with amount 10. For type=2 the nearest date to '2011-03-01' is 2011-02-01 with amount 20. And for type=3 the nearest to '2011-03-01' is the '2011-03-01' so it returns amount=30.
那么如何构造MySQL查询呢?
最佳答案
现在我只能想到一个子选择版本:
SELECT type, amount
FROM table AS t
WHERE dat = (
SELECT MAX(dat)
FROM table
WHERE type=t.type
AND dat <= '2011-03-01'
)
关于mysql - 获取最接近按类型分组的特定日期的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5565360/