PHP / SQL总客户数

时间:2016-01-06 05:20:08

标签: php mysql

由于各种原因,我的数据库有重复项,我试图为每个用户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),它将显示客户列表,并给我db行计数{{ 1}}。这就是我需要的数字。

在PhpMyAdmin中查询我运行

67

输出

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 | +------------------+----------------------+----------------+ 值的总总和进行求和,然后将该值作为Count defined as 'CustomerCount'

回显

我哪里错了?

感谢您的帮助:)。

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

1 个答案:

答案 0 :(得分:2)

只需将mysql_num_rows与结果一起使用:

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