星期一, 7月 02, 2012

mysql常用日期query

  1. 當天的資料
    select * from table where to_days(column_time) = to_days(now());
    select * from table where date(column_time) = curdate(); 
  2. Between日期
    WHERE (created BETWEEN '2012-06-19' and '2012-06-30 23:59:59' ); 
    注意結尾是最後一天一秒,否則只會出現到前一天的資料
  3. 最近7天,1個月
    WHERE DATE_SUB( CURDATE( ) , INTERVAL 7 DAY ) <= `created_date`;
    WHERE DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= `created_date`;
  4. 本周
    從當週的Sunday開始到星期六的七天資料
    select * from wap_content where week(created_at) = week(now);
    
Reference

沒有留言: