最近有個功能需要查詢資料庫中某天内的資料,而資料的時間是timestamp 格式,這樣我在查詢時,就有2中選擇
select * from tuples where datediff(created_on,'2008-07-09')=0
select * from tuples where date_format(created_on,'%Y-%m-%d')='2008-07-09'
資料庫中的資料量很大,超過400萬條。對比了下執行時間
Showing rows 0 - 29 (175 total, Query took 0.0009 sec)
Showing rows 0 - 29 (175 total, Query took 0.0012 sec)
從結果來看,他們相差不大,兩種都可接受。datediff似乎效率好點!