如果我选择日期20160730(第30周),它将在dayRemark表中显示第30周的所有dayID。
另外,我也希望限制中间3的结果。 示例:如果结果是:20160725,20160727,20160728,20160730,20160731。 我希望我的最终结果是:20160728,20160730,20160731(因为我的第一个选定日期是20160730)
以下是我的代码
SELECT dayRemark.dayID, calander.week_name,
FROM dayRemark
INNER JOIN calander
ON dayRemark.dayID = calander.dayID
where dayRemark.dayID = '20160730'
group by dayRemark.dayId
我如何将calander.week
和限制放在中间3?
答案 0 :(得分:1)
您可以使用LIMIT
和dayID
来选择dayID
比所请求的日期更重要的N行,并对(
SELECT dayRemark.dayID, calendar.week_name
FROM dayRemark
INNER JOIN calendar ON dayRemark.dayID = calendar.dayID
where dayRemark.dayID < '20160730'
group by dayRemark.dayId
ORDER BY dayRemark.dayID DESC
LIMIT 1)
UNION (
SELECT dayRemark.dayID, calendar.week_name
FROM dayRemark
INNER JOIN calendar ON dayRemark.dayID = calendar.dayID
where dayRemark.dayID = '20160730'
group by dayRemark.dayId
)
UNION (
SELECT dayRemark.dayID, calendar.week_name
FROM dayRemark
INNER JOIN calendar ON dayRemark.dayID = calendar.dayID
where dayRemark.dayID > '20160730'
group by dayRemark.dayId
ORDER BY dayRemark.dayID ASC
LIMIT 1
)
的N行执行相同操作更小在你的情况下N = 1所以你有:
None
不确定这是最好的方法。