1

My database has duplicates for different reasons, and I am trying to get a total of unique customers per a users 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.

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. That is the number I need.

Query in PhpMyAdmin I run

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;

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#.

16
  • please can you clear currently what you got in your necessary parameter and what you need ? Commented Jan 6, 2016 at 5:26
  • I have updated my OP with the descrambled sql query. @jilesh Commented Jan 6, 2016 at 5:28
  • you need total unique customer count ? am i right ? Commented Jan 6, 2016 at 5:32
  • I need to count unique customers, then sum total all customers total Commented Jan 6, 2016 at 5:33
  • but you used groupby so it gave you customer count 1 always Commented Jan 6, 2016 at 5:35

1 Answer 1

2

Just use mysql_num_rows with result:

$ApprovedCustomerResult = mysql_query($ApprovedCustomerCount);
$CustomerApprovedCount = mysql_num_rows($ApprovedCustomerResult);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.