0

我有一个广义搜索,我正在查找一周中不同日子的平均价格。问题是我不确定如何要求奇数范围。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)
4

2 回答 2

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)
于 2009-10-20T15:23:01.087 回答
2

使用“in”子句,如下所示:dayofweek(bookdate) in (1,2,7)

这也允许非连续日期的灵活性(例如,仅选择周一和周五的记录)

于 2009-10-20T15:23:05.703 回答