MYSQL select DISTINCT values in two columns

How about using GROUP BY?

SELECT foo,bar FROM my_table GROUP BY foo,bar


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;

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

very very wicked & evil:

select distinct
    least(foo, bar) as value1
  , greatest(foo, bar) as value2
from table

Tags:

Mysql

Sql