gpt4 book ai didi

PHP/SQL Sum 客户总数

转载 作者:行者123 更新时间:2023-11-29 06:18:20 25 4
gpt4 key购买 nike

由于不同的原因,我的数据库有重复项,我正在尝试获取每个用户 ID 的唯一客户总数。

这是我构建的当前查询。

    $ApprovedCustomerCount ="SELECT `".Customer_Full_Name."`, `".Customer_Address."`, COUNT(DISTINCT(`".Customer_Full_Name."`)) AS CustomersCount 
FROM `".OrdersTable."`
WHERE `".Users_Sales_ID."`=$EMPID
AND `".Current_Order_Stage."`='".Current_Order_Stage_Approved."'
GROUP BY `".Customer_Full_Name."`, `".Customer_Address."`
HAVING COUNT(".Customer_Full_Name.") > 1";

$ApprovedCustomerResult=mysql_query($ApprovedCustomerCount);
while($OrdersRow=mysql_fetch_array($ApprovedCustomerResult))
$CustomerApprovedCount = $OrdersRow['CustomersCount'];

if (empty($CustomerApprovedCount)) { $CustomerApprovedCount = '0'; }
echo $CustomerApprovedCount;

错误的是,当我在 PHP 中回显时,它只给我 1 的值。

但是,当我用确切的查询查询数据库表时,我得到一个输出(对于每个客户名称,唯一计数的地址 = 1),它将显示客户列表,并给我 67。这就是我需要的数字。

在我运行的 PhpMyAdmin 中查询

SELECT `CustomerName`, `CustomerAddress`, COUNT(DISTINCT(`CustomerName`)) AS Customers 
FROM `wrightway_orders`
WHERE `Employee_ID` = '3020'
AND `Order Stage`='Approved'
GROUP BY `CustomerName`, `CustomerAddress`
HAVING COUNT(*) > 1

输出

+------------------+----------------------+----------------+
| Customer Name | Address | CustomersCount |
+------------------+----------------------+----------------+
| ADRIANE JOHNSON | 10015 161ST PL NE | 1 |
+------------------+----------------------+----------------+
| BILL SMITH | 9923 161ST AVE NE | 1 |
+------------------+----------------------+----------------+
| BRIAN WALTERS | 11129 106TH AVE NE | 1 |
+------------------+----------------------+----------------+

等等

我需要对 定义为“CustomerCount” 值的 总数 求和,然后将该值回显为 echo $CustomerApprovedCount;

我哪里错了?

感谢您的帮助:)

我需要计算所有唯一的客户,然后给出 Employee_ID=ID# 的所有客户的总和。

最佳答案

只需使用 mysql_num_rows 结果:

$ApprovedCustomerResult = mysql_query($ApprovedCustomerCount);
$CustomerApprovedCount = mysql_num_rows($ApprovedCustomerResult);

关于PHP/SQL Sum 客户总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34626129/

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