这是我撰写的查询,目的是为您提供最近的星期四和结尾的星期三
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
这是今天的一个例子,2011-09-21
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2011-09-15 00:00:00 | 2011-09-21 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
只需将NOW()函数调用替换为所需的任何日期时间,您就可以选择从星期四开始的一周中的任意一个给定日期时间。
这是另一个使用特定日期“ 2011-01-01”的示例
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE('2011-01-01') - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE('2011-01-01') dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2010-12-30 00:00:00 | 2011-01-05 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
您table
今天查询的查询类似于以下内容:
SELECT * from `table`,
(SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A) M
WHERE `date` >= thu_beg
AND `date` <= wed_end;
试试看 !!!
更新2011-09-22 16:27 EDT
这是我建议的标记星期四的查询。
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
其他星期怎么样?
(SELECT SUBSTR('6012345',wkndx,1)
从星期一开始到星期日结束的一周
(SELECT SUBSTR('5601234',wkndx,1)
从星期二开始到星期一结束
(SELECT SUBSTR('4560123',wkndx,1)
从星期三开始到星期二结束
(SELECT SUBSTR('3456012',wkndx,1)
从星期四开始到星期三结束
(SELECT SUBSTR('2345601',wkndx,1)
从周五开始到周四结束
(SELECT SUBSTR('1234560',wkndx,1)
从周六开始到周五结束
(SELECT SUBSTR('0123456',wkndx,1)
周日开始于星期日结束吗