Mysql: Select Unique Value

Mysql: Select Unique Value

In my table I have several duplicates. Ineed to find unique values in mysql table column.

SQL

SELECT column FROM table
WHERE column is unique

SELECT column FROM table
WHERE column = DISTINCT

I've been trying to Google, but almost all queries are more complex.

The result I's like is all non duplicate values.

EDIT I'd like to have UNIQUE values...

Not all values one time... (Distinct)

1

4 Answers

Try to use DISTINCT like this:

SELECT DISTINCT mycolumn FROM mytable

EDIT:

Try

select mycolumn, count(mycolumn) c from mytable
group by mycolumn having c = 1
7

Here is the query that you want!

SELECT column FROM table GROUP BY column HAVING COUNT(column) = 1

This query took 00.34 seconds on a data set of 1 Million rows.

Here is a query for you though, in the future if you DO want duplicates, but NOT non-duplicates...

SELECT column, COUNT(column) FROM table GROUP BY column HAVING COUNT(column) > 1

This query took 00.59 seconds on a data set of 1 Million rows. This query will give you the (column) value for every duplicate, and also the COUNT(column) result for how many duplicates. You can obviously choose not to select COUNT(column) if you don't care how many there are.

You can also check this out, if you need access to more than just the column with possible duplicates... Finding duplicate values in a SQL table

2

Try this one:

SELECT COUNT(column_name) AS `counter`, column_name 
FROM tablename 
GROUP BY column_name 
WHERE COUNT(column_name) = 1

Have a look at this fiddle:

2

Try this:

SELECT DISTINCT (column_name) FROM table_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.