My SQL 查詢當天、昨天、近七天、當月
阿新 • • 發佈:2018-12-13
查詢當天的所有資訊:
select * from tongxunlog where date_sub('2018-10-04',interval 0 day)
或者 Ps:date(你要查詢的表名)
select * from tongxunlog where date(fasongshijian) = curdate()
或者
select * from tongxunlog where to_days(fasongshijian) = to_days(now());
查詢昨天的所有資訊:
select * from tongxunlog where date(fasongshijian) = date_sub(curdate(),interval 1 day);
查詢昨天和今天的所有資訊:
select * from tongxunlog where to_days(now()) - to_days(fasongshijian) <= 1
查詢近七天的所有資訊:
select * from tongxunlog where date(fasongshijian) >= date_sub(curdate(),interval 7 day) and date(fasongshijian) <= date_sub(curdate(),interval 1 day)
或者 (簡單版寫法)
select * from tongxunlog where date_sub(curdate(), interval 7 day) <= date(fasongshijian)
查詢進30天的所有資訊:
select * from tongxunlog where date_sub(curdate(), interval 30 day) <= date(fasongshijian)
查詢當月的所有資訊:
select * from tongxunlog where date_format(fasongshijian,'%Y%m') = date_format(curdate(),'%Y%m')