Bernie
Bernie

Reputation: 1651

Avoid duplicates in SELECT

possible a super simple question, but I just don't get it now. Here we go: I have a table

id | lang1  | sth | sth2
1  | "one"  | "x" | "y"
1  | "one"  | "x" | "y"
2  | "two"  | "y" | "z"
1  | "one"  | "x" | "y"
3  | "three"| "z" | "a"

And I need a SELECT, that gives me everything without duplicates.

1  | "one"  | "x" | "y"
2  | "two"  | "y" | "z"
3  | "three"| "z" | "a"

So possibly something like (Pseudo code, doesn't work, just for the idea)

SELECT id, lang1, sth, sth2 FROM tbl GROUP BY id, lang1

OR

SELECT DISTINCT(id, lang1), sth, sth2 FROM tbl

Thanks, Bernd

Upvotes: 0

Views: 60

Answers (2)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

DISTINCT applies to all expression/column so use it

select distinct id, lang1, sth, sth2
from table t;

However, GROUP BY clause applies whenever you have to find identical values

So, you can express

select id, lang1, sth, sth2
from table t
group by id, lang1, sth, sth2;

Upvotes: 3

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

Just plain simple DISTINCT will do

SELECT DISTINCT id, lang1, sth, sth2
FROM tbl

DISTINCT(id, lang1) won't work because DISTINCT is a keyword, not a function. Function calls in SQL have parentheses.

Upvotes: 6

Related Questions