sql語句查詢某年某月某日。以及,昨天,最近一週內,一月內,上個月

因爲數據庫中一般存的都是unix類型的時間,所以要事先轉換一下 


查詢固定某一天的語句:

select  *  from  `表名`  where from_unixtime(時間字段名,'%Y-%m-%d') =('2017-03-29')


查詢今天,昨天,最近7天,最近30天,本月,上月的語句:

$comsql= "select  *  from ` 表名` ";

$Today=   "  where to_days(from_unixtime(時間字段名)) = to_days(now())";

$Ysterday="  where to_days(now()) - to_days(from_unixtime(時間字段名)) = 1";  

$Week=    "  where date_sub(curdate(), INTERVAL 7 DAY) <= date(from_unixtime(時間字段名))";

 $Zuijin=  "  where date_sub(curdate(), INTERVAL 30 DAY) <= date(from_unixtime(時間字段名))";

$Month=   "  where date_format(from_unixtime(時間字段名), '%Y%m') = date_format(curdate() , '%Y%m')";

$LastMonth="   where period_diff(date_format(now() , '%Y%m') , date_format(from_unixtime(時間字段名), '%Y%m')) =1";    



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章