How can i get last week, current week and last month record from mysql

假如想象 提交于 2019-12-12 17:10:16

问题


i am storing date in strtotime format in codeigniter. and week start from sunday.

$this->db->where($this->weight . '.create_date <=', 'curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY');
$this->db->where($this->weight . '.create_date >', 'curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY');

回答1:


For taking last week:

WHERE date BETWEEN date_sub(now(),INTERVAL 1 WEEK) and now();

For taking current week:

WHERE  YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)

For taking last month:

year(date(FROM_UNIXTIME(u.create))) = year(CURDATE() - INTERVAL 1 MONTH)
and month(date(FROM_UNIXTIME(u.create))) = month(CURDATE() - INTERVAL 1 MONTH)


来源:https://stackoverflow.com/questions/28936174/how-can-i-get-last-week-current-week-and-last-month-record-from-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!