Q:

select distinct pair values mysql

i have the following table and I'm hoping to extract all the unique pairs for ex: 
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    1 |    3 |
|    1 |    6 |
|    1 |    9 |
|    2 |    1 |
|    2 |    3 |
|    3 |    1 |
|    3 |    2 |
|    4 |    1 |
|    6 |    5 |
+------+------+

the out put should be
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    1 |    3 |
|    1 |    6 |
|    1 |    9 |
|    2 |    3 |
|    6 |    5 |
+------+------+

A:

Try this:
select least(a, b) as a, greatest(a, b) as b
from table t
group by least(a, b), greatest(a, b);

This could produce output that is not in the original table (such as 5/6 instead of 6/5).  If you want to preserve the original order:
select distinct a, b
from table t
where a <= b or
      not exists (select 1 from table t2 where t2.b = t.a and t2.a = t.b);

That is, select all pairs where a is less than or equal to b or a is greater than b and no row exists with the values in the other order.

