[英]Laravel 5 Multi Select not working
I need help with setting this one query in laravel 5. 我需要在laravel 5中设置此查询的帮助。
This is my table example... : 这是我的表格示例...:
table name: mytable 表名: mytable
user_id cost status created_at
--------------------------------------
1 10 Pending 2016-04-21 04:51:01
1 20 Pending 2016-04-22 04:51:01
1 100 Approved 2016-04-25 04:51:01
1 45 Pending 2016-04-28 04:51:01
1 60 Approved 2016-05-01 04:51:01
1 60 Approved 2016-05-01 04:51:01
1 160 Approved 2016-05-03 04:51:01
1 360 Pending 2016-05-05 04:51:01
1 260 Approved 2016-05-12 04:51:01
1 210 Pending 2016-05-21 04:51:01
basically I need One SQL statement in Laravel that will output something like this or close per user , because I might have different user ids 基本上我需要在Laravel中使用一条SQL语句,该语句将输出类似的结果或每个用户关闭,因为我可能具有不同的用户ID
Totalpending Totalapproved TotalCost
April/2016 3 1 ( add cost for April)
May/2016 1 4 ( add all cost for May)
I have this code so far , but how to make it all in one query ? 到目前为止,我已经有了这段代码,但是如何在一个查询中全部完成呢?
$query = DB::table('my_table')->select(DB::raw('MONTH(created_at) as m, YEAR(created_at) as y'), DB::raw('count(*) as pp'))->where('user_id' ,Auth::id())->where('status','Pending')
->groupBy('created_at')
->get();
var_dump($query);
But this outputs only pending like below: ... 但这仅输出如下所示:...
array(1) {
[0]=>
object(stdClass)#321 (3) {
["m"]=>
string(1) "4"
["y"]=>
string(4) "2016"
["pp"]=>
string(1) "3"
}
}
or I can have an output like this : where "pp" > total pending , "aa" > total approved , "cc" > total cost 或者我可以有这样的输出:其中“ pp”>待处理的总金额,“ aa”>批准的总金额,“ cc”>总费用
array(1) {
[0]=>
object(stdClass)#321 (3) {
["m"]=>
string(1) "4"
["y"]=>
string(4) "2016"
["pp"]=>
string(1) "3"
["aa"]=>
string(1) "1"
["cc"]=>
string(1) "175"
}
}
How to show the others approved and total cost?? 如何显示其他已批准的费用和总费用?
If you were implenting this using SQL only, then something like below should help you. 如果仅使用SQL来阻止这种情况,那么下面的内容应该会对您有所帮助。
SELECT CONCAT_WS('/', MONTHNAME(`created_at`), YEAR(`created_at`)) AS `Datum`,
SUM(CASE WHEN `status` = 'Pending' THEN 1 ELSE 0 END) AS `TotalPending`,
SUM(CASE WHEN `status` = 'Approved' THEN 1 ELSE 0 END) AS `TotalApproved`,
SUM(`cost`) AS `TotalCost`
FROM `mytable`
GROUP BY `Datum`
ORDER BY `Datum` DESC;
But since you are using the Laravel's query builder, I guess it can be ported like the one below. 但是,由于您使用的是Laravel的查询生成器,因此我想可以像下面那样移植它。
$query = DB::table('mytable')->select(
DB::raw(
'CONCAT_WS(\'/\', MONTHNAME(created_at), YEAR(created_at)) as Datum,
SUM(CASE WHEN status = \'Pending\' THEN 1 ELSE 0 END) AS TotalPending,
SUM(CASE WHEN status = \'Approved\' THEN 1 ELSE 0 END) AS TotalApproved,
SUM(cost) as TotalCost'
)
)->where('user_id', Auth::id())->groupBy('Datum')->orderBy('Datum', 'desc')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.