Skip to content

howto - mysql count with unique entries only

Ever tried to count for a number of entries and figured out that the opposite/control query does not provide a fitting number? Maybe you forgot about the fact, that you have selected multiple entries per id. You can easily adapt your query to count only unique entries.

SELECT COUNT(DISTINCT `my_table`.`my_column`) FROM `my_table` AS `t` [ LEFT JOIN `my_second_table` AS `t2` ON (`t`.`id` = `t2`.`my_table_id` AND `t2`.`is_online` = 1) ] [ WHERE `t`.`my_column` = 'my_value` ]
I have also added this to my github howtos, fork for free ;-). sql count function with examples