最近遇到一个需要判断在某一时间段条件的查询,最开始用的between..and,后来想了想为啥不能用小于、大于,然后又想到用datediff函数先求出两个时间差再做正负判断。
后来再了一次测试,数据比较少,大概500条数据,结果如下:
SELECT * FROM `userleveldetaillog` as u where u.`operatedate` >'2010-03-01' AND u.`operatedate`<'2010-04-01';
415 rows fetched (31 ms)
SELECT * FROM `userleveldetaillog` as u where u.`operatedate` between '2010-03-01' AND '2010-04-01';
415 rows fetched (47 ms)
SELECT * FROM `userleveldetaillog` as u where DATEDIFF(u.`operatedate`,'2010-03-01')>0 AND DATEDIFF(u.`operatedate`,'2010-04-01')<0;
415 rows fetched (62 ms)
结果在一定程度上说明:
[color=red]还是土方法效率高。。。[/color]