Jieqin
Jieqin

Reputation: 588

How Do I Select Unique Combinations From MySQL?

Let's say i have these in my table.

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

I want to only display these

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

because the bottom 3 area all duplicates if you flip them around. But i don't want rows 1,2 to be hidden as well because it's also a combination of water and cold.

Upvotes: 0

Views: 53

Answers (1)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171351

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

SQL Fiddle Example

Upvotes: 6

Related Questions