laravel 如何计算eloquent中的百分比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48041121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:11:00  来源:igfitidea点击:

How to calculate the percentage in eloquent?

phpmysqllaraveleloquent

提问by Shakil Ahmed

please have a look at the problem. You will find it interesting. I have a table memberin which I have a column result. In this result column three values can be possible and at one time only one value can be used. The value that are possible W, Land V. Forget about the Vas it is not required here.

请看一下问题。你会发现它很有趣。我有一张桌子member,里面有一列result。在此结果列中,可以使用三个值,但一次只能使用一个值。可能的值WLV。忘记V这里,因为它不是必需的。

Let's say, I have 5 rows and in 3 rows I have Wand in 1 row I have Land last one is V.

比方说,我有 5 行,我有 3 行,我有W1 行L,最后一个是V.

So here 3W and 1L out of 4(W+L together). So the percentage of W is 75%. How can I calculate it using mysql and if it is possible using eloquent would be better.

所以这里是 3W 和 1L,共 4 个(W+L 加在一起)。所以W的百分比是75%。我如何使用 mysql 计算它,如果可以使用 eloquent 会更好。

Currently, I am using this solution 1. Getting all the rows 2. Running php loop 3. And based on the value of the result column I am calculating the percentage. works fine. But I think if there are better solution why not to find it :)

目前,我正在使用此解决方案 1. 获取所有行 2. 运行 php 循环 3. 根据结果列的值,我正在计算百分比。工作正常。但我认为如果有更好的解决方案为什么不找到它:)

Hope you have something for this problem. Thanks in advance.

希望你有办法解决这个问题。提前致谢。

回答by Tim Biegeleisen

Your question is a bit vague, but from what I see the COUNTfunction should be sufficient here:

你的问题有点含糊,但从我看来,COUNT这里的功能应该足够了:

SELECT
    100.0 * COUNT(W) / COUNT(*) AS w_pct,
    100.0 * COUNT(L) / COUNT(*) AS l_pct
FROM yourTable;

Of course, if you are already doing an aggregation then we can modify the above query to include GROUP BY.

当然,如果您已经在进行聚合,那么我们可以将上述查询修改为 include GROUP BY

回答by Alexey Mezenin

If you want a pure Eloquent solution, do this:

如果您想要一个纯粹的 Eloquent 解决方案,请执行以下操作:

$wl = Member::whereIn('result', ['W', 'L'])->count();
$total = Member::count();
$percent = $wl / $total * 100;

This will be much more efficient than actually retrieving any records from DB.

这将比实际从数据库中检索任何记录更有效。

回答by Amir Khan

And what if the $total=0? In this scenario dividing by zero will give an error. Make sure to check it as well like:

如果 $total=0 呢?在这种情况下,除以零会产生错误。确保也检查它,例如:

if ($total != 0) {
$percent = $wl / $total * 100;
} else {
$percent = 0;
}

I hope you will find it helpful in the future.

我希望你将来会发现它很有帮助。

回答by Php Developer

In Laravel Join query with count percentage:

在带有计数百分比的 Laravel Join 查询中:

select('marketplace.store_name', 'users.name' , **DB::raw('round((count(products.user_id)/(select count(*) from products)*100),2) as percentage')** , DB::raw('round(avg(products.user_id),2) as avg') );

I hope this helps.

我希望这有帮助。