我有两个表,即View表和Click Table,如下所示
Created_at View_count Click_count
January 10 20
Febrauary 20
March 30 40
May 30
我想根据月份获取计数数据。通过查询,我想得到如下表格信息。
$view_count= View::select(DB::raw("count(*) as count"))
->orderBy("created_at")
->groupBy(DB::raw("month(created_at)"))
->get()->toArray();
$view_count= array_column($view_count, 'count');
$click_count= Click::select(DB::raw("count(*) as count"))
->orderBy("created_at")
->groupBy(DB::raw("month(created_at)"))
->get()->toArray();
$click_count= array_column($click_count, 'count');
$count_bymonth = View::select(DB::raw('MONTHName(created_at) as month'))
->groupBy(DB::raw(' MONTH(created_at)'))
->get()->toArray();
$count_bymonth = array_column($count_bymonth , 'month');
return view('testing')
->with('view_count', json_encode($view_count, JSON_NUMERIC_CHECK))
->with('click_count', json_encode($click_count, JSON_NUMERIC_CHECK))
->with('count_bymonth', json_encode($count_bymonth, JSON_NUMERIC_CHECK));
我试过这样的话:
#!/bin/sh
# Use in root dir of git repository
# Fixes some newline-related weirdness
git rm --cached -r .
git reset --hard
如果有人知道这方面的答案,请尽快告诉我。
三江源。
答案 0 :(得分:1)
您可以在一个查询中执行此操作
$result = View::join('Click','Click.Created_at','=','View.Created_at')
->select(
'Click_count.Created_at'
DB::raw('SUM(Click_count) as Click_count'),
DB::raw('SUM(View_count) as View_count')
)
->groupBy('Click.Created_at')
->orderBy("created_at")
->get();
return view('testing')->with('result');
然后你要做的就是循环result
。
答案 1 :(得分:0)
使用laravel查询构建器连接
DB::table('View')
->join('Click', 'View.Created_at', '=', 'Click.Created_at')->get();