Mysql(Laravel)實現按天分組查詢資料 – DB::raw – FROM_UNIXTIME()- UNIX_TIMESTAMP()

Mysql(Laravel)實現按天分組查詢資料 – DB::raw – FROM_UNIXTIME()- UNIX_TIMESTAMP()

格式化時間進行查詢

Laravel框架查詢:

$query_send = Send::select(DB::raw(“FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),’%Y-%m-%d’) as riqi”))->groupBy(DB::raw(“FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),’%Y-%m-%d’)”))->orderBy(“riqi”,”desc”)->take(10)->get();//take(10)獲取最近10條

MySQL查詢:

select FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),’%Y-%m-%d’) as riqi from `hx_send` group by FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),’%Y-%m-%d’) order by `riqi` desc limit 10

再按天查詢每天資料