gpt4 book ai didi

php - 对sql中的查询结果进行分组

转载 作者:行者123 更新时间:2023-11-29 12:01:03 26 4
gpt4 key购买 nike

例如,我有一列:bank_id 和 status。

使用查询:

SELECT Bank_ID, status FROM int_client_bank WHERE status = 30 or status = 50 or status = 35 or status = 37;

我得到的结果是:

例如。

**id_bank status**
1, 30
1, 30
1, 50
1, 35
2, 50
2, 37

等等..

我需要进行一个查询,如果有状态百分比,每个银行都可以对我进行计数,然后使用 php/symfony 将其扔到表中。

等:1 家银行:-

status30 - 2 each (20%)
status50 - 4 each (40%)
status - 6 each (60%)

2家银行等等..

怎么做?

最佳答案

在 SQL 中使用分组按银行和状态进行分组,并对每个组中的行进行计数:

SELECT Bank_ID, status, COUNT(Bank_ID) count FROM int_client_bank WHERE status = 30 or status = 50 or status = 35 or status = 37 GROUP BY Bank_ID, status;

然后,您可以对所有计数求和,并为每行计算百分比,例如 $row['count']/$all_count * 100

关于php - 对sql中的查询结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com