简体   繁体   English

PHP / SQL客户总和

[英]PHP/SQL Sum total customer count

My database has duplicates for different reasons, and I am trying to get a total of unique customers per a users ID. 由于不同的原因,我的数据库存在重复项,因此,我试图通过一个用户ID获得总计唯一身份的客户。

This is the current query that I have constructed. 这是我构建的当前查询。

    $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;

Mistakenly, it only gives me a value of 1 when I echo in PHP. 错误地,当我在PHP中回显时,它只给我一个值1

However, when I query the DB Table with the exact query I get an output ( for each customer name, address that are unique counts = 1) that will display the list of customers, and give me db row count of 67 . 但是,当我使用确切的查询查询数据库表时,我得到一个输出(对于每个客户名,唯一计数的地址= 1),该输出将显示客户列表,并给我db行计数67 That is the number I need. 那是我需要的电话号码。

Query in PhpMyAdmin I run 在我运行的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 

Outputs 输出

+------------------+----------------------+----------------+
|  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       |
+------------------+----------------------+----------------+

etc 等等

I need to sum the total amount of Count defined as 'CustomerCount' values to then echo that value as echo $CustomerApprovedCount; 我需要对Count defined as 'CustomerCount'值的Count defined as 'CustomerCount' 总数求和 ,然后将该值作为echo $CustomerApprovedCount;

Where am I going wrong? 我要去哪里错了?

I appreciate your help :) 我感谢您的帮助 :)

I need to count all customers that are unique, then give a sum total of all customers where Employee_ID=ID#. 我需要计算所有唯一的客户,然后给出Employee_ID = ID#的所有客户的总和。

Just use mysql_num_rows with result: 只需将mysql_num_rows与结果配合使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM