Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count total without duplicate using MySQL and PHP

Tags:

php

mysql

count

I am attempting to query a MySQL database 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, that is, Total Returned: 7

like image 902
Jeremy Avatar asked Nov 24 '25 06:11

Jeremy


1 Answers

This DISTINCT in this query:

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.

like image 62
Sebas Avatar answered Nov 25 '25 18:11

Sebas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!