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

Translate to de es fr it pt ja

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Markdown format allowed
Form options