如何在mysql中加倍内连接

时间:2016-08-01 16:20:14

标签: mysql

This is my table

如果我选择日期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?

1 个答案:

答案 0 :(得分:1)

您可以使用LIMITdayID来选择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

不确定这是最好的方法。