如何从MySQL中选择唯一的组合?

时间:2012-10-01 14:32:20

标签: mysql

假设我在桌子上有这些。

Index   Resultant   First   Second
1       Ice         Water       Cold
2       Cold Air    Water       Cold
3       Cold Wind   Water       Cold
4       Hot Air     Volcano     Air
5       Normal Air  Oxygen      Hydrogen
6       Ice         Cold        Water
7       Cold Air    Cold        Water
8       Cold Wind   Cold        Water

我想只显示这些

Index   Resultant   First   Second
1       Ice         Water       Cold
2       Cold Air    Water       Cold
3       Cold Wind   Water       Cold
4       Hot Air     Volcano     Air
5       Normal Air  Oxygen      Hydrogen

因为底部3区域全部重复,如果你翻转它们。但我不希望第1,2行被隐藏,因为它也是水和冷的组合。

1 个答案:

答案 0 :(得分:6)

select least(col1, col2), greatest(col1, col2)
from MyTable
group by least(col1, col2), greatest(col1, col2)

SQL Fiddle Example