Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL select DISTINCT values in two columns

Tags:

sql

mysql

I want to select distinct values in a database. Let me run you through a quick example.

Table:

foo bar
--- ---
a   c
c   f
d   a
c   a
f   c
a   c
d   a
a   c
c   a
f   c

Right, let's say my SQL is SELECT DISTINCT foo, bar from table. These are my results:

foo bar
--- ---
a   c
c   f
d   a
c   a
f   c

However the problem is is that there are repetitions of a c / c a just that they are in a different order. I don't want to select these, I want distinct values from both columns, please help!

like image 343
Jake Avatar asked Jul 19 '11 09:07

Jake


People also ask

How do I select distinct two columns in MySQL?

Select with distinct on all columns of the first query. Select with distinct on multiple columns and order by clause. Count() function and select with distinct on multiple columns.

Can we use distinct on multiple columns in MySQL?

MySQL DISTINCT with multiple columnsWhen you specify multiple columns in the DISTINCT clause, the DISTINCT clause will use the combination of values in these columns to determine the uniqueness of the row in the result set.

How do I select distinct values from multiple columns in SQL?

Using code SELECT DISTINCT(id,col_a) FROM Table will result: "(2,2)" "(3,3)" "(4,3)" "(5,4)" as you can see, the second column has duplicates.

Does select distinct apply to all columns?

Yes, DISTINCT works on all combinations of column values for all columns in the SELECT clause.


7 Answers

How about using GROUP BY?

SELECT foo,bar FROM my_table GROUP BY foo,bar

like image 103
Yaniv Avatar answered Sep 29 '22 06:09

Yaniv


very very wicked & evil:

select distinct
    least(foo, bar) as value1
  , greatest(foo, bar) as value2
from table
like image 43
nabuchodonossor Avatar answered Sep 27 '22 06:09

nabuchodonossor


How about :

SELECT DISTINCT a.foo,a.bar 
FROM table a
LEFT JOIN table b ON a.foo=b.bar and a.bar=b.foo
WHERE b.foo IS NULL AND b.bar IS NULL

Output :

foo bar
--- ---
d   a
like image 29
Kevin Burton Avatar answered Oct 01 '22 06:10

Kevin Burton


You're asking for something that's the opposite of a symmetric closure (I don't know if it has a special name; antisymmetric something, since it's not a closure). For closures and closure like things where you need to compare two different columns, you can use joins. To make sure you don't filter out both rows when they are duplicated across columns, you need to a way to differentiate the repeats and include one of them, such as by including the pair where the first is the lesser.

SELECT DISTINCT t1.foo, t1.bar
  FROM `table` t1
    LEFT JOIN `table` t2
      ON t1.foo=t2.bar AND t1.bar=t2.foo 
  WHERE t2.foo IS NULL OR t1.foo <= t1.bar;
like image 25
outis Avatar answered Sep 30 '22 06:09

outis


   SELECT 
       foo, bar
   FROM tableX
   WHERE foo <= bar
 UNION 
   SELECT 
       bar, foo
   FROM tableX
   WHERE bar < foo
like image 30
ypercubeᵀᴹ Avatar answered Sep 30 '22 06:09

ypercubeᵀᴹ


SELECT DISTINCT foo, bar FROM table WHERE
CONCAT(',',foo,bar,) NOT IN ( SELECT CONCAT(',',bar,foo) FROM table )
like image 30
Talha Ahmed Khan Avatar answered Sep 30 '22 06:09

Talha Ahmed Khan


This works for me:

SELECT DISTINCT
LEAST(sub.foo, sub.bar) as value_1
, GREATEST(sub.foo, sub.bar) as value_2

FROM
(SELECT
a.foo
,a.bar
FROM
table a
JOIN
table b
on a.foo = b.bar
and a.bar = b.foo) sub
like image 21
bfortuner Avatar answered Sep 29 '22 06:09

bfortuner