65

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
  • 2
    SELECT DISTINCT columnname FROM table Commented Dec 16, 2015 at 13:02

4 Answers 4

110

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 
Sign up to request clarification or add additional context in comments.

7 Comments

I cannot use this. Because it will give me all values from column. I´d like to filter out the values that is duplicates. So that i select values that only appears once!
@Mjukis:- Updated my answer. Also your question has no explanation about what you said in comments! :(
@Mjukis:- saying unique values does not make sense. You need to explicitly state that you are looking for non duplicate values excluding duplicates
ok Master... the updated code works fine. Thank you very much!
I don't get it "group by mycolumn having c = 1"
|
16

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 Comments

if you DO want duplicates, but NOT non-duplicates... what does that mean?
@TheRealChx101 So, OP wants to see all values where there is NOT a duplicate of it anywhere. I showed that in query 1. In my 2nd query I show how you could show every value that DOES have a duplicate, and hide ones that do not. So I showed how to do what they want, and the opposite of what they want too.
2

Try this:

SELECT DISTINCT (column_name) FROM table_name 

Comments

1

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: http://sqlfiddle.com/#!9/15147/2/0

2 Comments

Where should i use "counter" ?
counter is simple a help column to show the number of occurrence for a value. Have a look at the fiddle :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.