I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

SELECT DISTINCT `col` WHERE (`col` is not unique)

How can I select the distinct values from table.col where the value of table.col is not unique.

Consider the example table on this page: (w3schools)

I want to return a list of countries where there's more than 1 customer. In this case it would return only "Mexico".

I am currently using 1 query to get the distinct values, then looping through and checking the number of occurrences with another query. Can this be done with a single query?

Upvotes: 2

Views: 1543

Answers (1)

Lee
Lee

Reputation: 1427

Use HAVING and GROUP BY

SELECT country FROM customers GROUP BY country HAVING Count(*)>1

Upvotes: 5

Related Questions