每个 GROUP BY 的总行数

发布于 2024-12-13 18:25:13 字数 265 浏览 2 评论 0原文

我正在尝试运行 MySQL 查询并使用 PHP 执行以下操作:

我需要将数据分组到列中 (GROUP BY),然后计算 (COUNT) 每组中的行数。之后,我需要将给定组中的行数除以组数,以获得该组的受欢迎程度百分比。

因此,如果我有一个包含以下数据的表:

Version_Number
1.1
1.2
1.1
1.2
1.2
1.1

I need the final output to be:

1.1 50%
1.2 50%

I'm trying to run a MySQL query and use PHP to do the following:

I need to group data in a column (GROUP BY) then count (COUNT) the number of rows in each group. After that, I need to divide the number of rows in a given group by the number of groups to get that groups percentage of popularity.

So if I had a table with the following data:

Version_Number
1.1
1.2
1.1
1.2
1.2
1.1

I need the final output to be:

1.1 50%
1.2 50%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心荒芜 2024-12-20 18:25:13

您可能需要针对 mysql 进行调整(我通常在 oracle 中工作),但是:

SELECT 
    ( count(*) / totalCount * 100) AS percentOfVersionedThings
    , Version_Number
FROM tableOfVersionedThings
    INNER JOIN ( SELECT count(*) as totalCount FROM tableOfVersionedThings ) ON 1=1
GROUP BY Version_Number

如果这是 Oracle,我建议使用分析,但我不确定 MySQL 中是否有等效的。也就是说,在您的情况下,一个简单的子查询应该可以解决问题,并且它应该可以在任何 SQL 数据库上运行。

You may need to adjust this for mysql (I normally work in oracle) but:

SELECT 
    ( count(*) / totalCount * 100) AS percentOfVersionedThings
    , Version_Number
FROM tableOfVersionedThings
    INNER JOIN ( SELECT count(*) as totalCount FROM tableOfVersionedThings ) ON 1=1
GROUP BY Version_Number

If this was Oracle, I'd suggest using analytics, but I'm not sure if there is an equivalent in MySQL. That said, in your case a simple sub-query should solve the problem, and it should be workable on any SQL database.

诗笺 2024-12-20 18:25:13

这就是我在尝试上述操作后最终所做的:

$result = mysql_query("SELECT COUNT(*) AS nsites FROM table WHERE auth = '$auth'") or
                    die(mysql_error());
                    $return_sites = mysql_fetch_array($result);

                if (!$return_sites['nsites']) {

                echo "...";

                } else {

                    $esult = mysql_query("SELECT * FROM table WHERE auth = '$auth' GROUP BY theme_version") or die(mysql_error());
                    while($row = mysql_fetch_array($esult)){                        

                        $get_current_version = $row['theme_version'];

                        $vresult = mysql_query("SELECT COUNT(*) AS nversion FROM table WHERE auth = '$auth' AND theme_version = '$get_current_version'") or die(mysql_error());
                        $version = mysql_fetch_array($vresult);

                        $percent = round($version['nversion'] / $return_sites['nsites'] * 100);

                            echo "...";

                    }

                }

This is what I ended up doing after trying the above:

$result = mysql_query("SELECT COUNT(*) AS nsites FROM table WHERE auth = '$auth'") or
                    die(mysql_error());
                    $return_sites = mysql_fetch_array($result);

                if (!$return_sites['nsites']) {

                echo "...";

                } else {

                    $esult = mysql_query("SELECT * FROM table WHERE auth = '$auth' GROUP BY theme_version") or die(mysql_error());
                    while($row = mysql_fetch_array($esult)){                        

                        $get_current_version = $row['theme_version'];

                        $vresult = mysql_query("SELECT COUNT(*) AS nversion FROM table WHERE auth = '$auth' AND theme_version = '$get_current_version'") or die(mysql_error());
                        $version = mysql_fetch_array($vresult);

                        $percent = round($version['nversion'] / $return_sites['nsites'] * 100);

                            echo "...";

                    }

                }
许一世地老天荒 2024-12-20 18:25:13

像这样的东西:

select v."Version_Number", count(v."Version_Number"),count(v."Version_Number")::float / (select count(v2."Version_Number")::float from versions v2 )::float * 100::float
from versions as v
group by v."Version_Number"

Something like this:

select v."Version_Number", count(v."Version_Number"),count(v."Version_Number")::float / (select count(v2."Version_Number")::float from versions v2 )::float * 100::float
from versions as v
group by v."Version_Number"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文