我有一个广义搜索,我正在查找一周中不同日期的平均价格。问题是我不确定如何要求奇数范围。 1-7工作正常,(星期一 - 星期日),但如果用户星期二通过星期日...即7-2它不起作用。
1-7(1,2,3,4,5,6,7) 7-2(7,1,2)
等
除了BETWEEN之外,我还能如何传递更智能的范围?
SELECT item_id, DAYOFWEEK(bookdate) as date, bookdate, AVG(price) AS price
FROM `availables` WHERE (item_id = 16 and DAYOFWEEK(bookdate) BETWEEN 2 AND 7)
GROUP BY DAYOFWEEK(bookdate)
答案 0 :(得分:3)
也许您正在寻找的是IN语法:
SELECT item_id, DAYOFWEEK(bookdate) as date, bookdate, AVG(price) AS price
FROM `availables` WHERE (item_id = 16 and DAYOFWEEK(bookdate) IN (7,1,2))
GROUP BY DAYOFWEEK(bookdate)
答案 1 :(得分:2)
使用'in'子句执行此操作,如下所示:(1,2,7)中的dayofweek(bookdate)
这也允许非连续日期的灵活性(例如,仅选择星期一和星期五的记录)