Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am attempting to query a mysql db with php to count the total number of entries without duplicates.

EXAMPLE
N111US
N111US
N123US
N345US
A6-EWD
A7-EEE
B-18701
N123US
N345AA

I wish to have the total number of the unique entries and I cannot begin to see where to start with the sql query IE: Total Returned: 7

share|improve this question
1  
add in the end of query group by coloumn – dianuj Apr 15 at 19:59

2 Answers

SELECT COUNT(DISTINCT `column`) AS `cnt` FROM `table`;

will eliminate duplicates.

Then, from php, fetch the cnt column value the way you would do for any other regular query.

share|improve this answer

I like the SQL answers posted above. They're efficient and performed on the query, itself. But if you have already read the items from the database into an array, you can use PHP's array_unique--then get a count of that result into an array with count. For example:

<?php
$input = array("a" => "dog", "cat", "b" => "cat", "dog", "mouse");
$result = array_unique($input);
$uniqueCount = count($result);
?>

Hope this helps. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.