I have 3 mysql tables (animal, category and animal_2_category). Every animal can have 0-n categories (for example fish, pet, insect, carnivore, "can fly", etc.).
Table "animal" (id, name)
1, dog
2, cat
3, bee
...
Table "categories" (id, name)
1, pet
2, insect
3, mammal
4, fish
5, can fly
...
Table "animal_2_category" (animal_id, category_id)
1, 1
1, 3
2, 1
2, 3
3, 2
3, 5
...
What I need now, is a list of all category combinations. The following query works:
SELECT CONCAT_WS("-", x.name, c.name)
FROM animal_2_category a2c1
JOIN animal_2_category a2c2 ON a2c1.animal_id = a2c2.animal_id
JOIN category c ON c.id = a2c2.category_id´
JOIN categories x
GROUP BY a2c2.category_id
This query wil return the following:
- pet-mammal
- mammal-pet
- insect-can fly
The problem with this is, that I get dublicate entries "pet-mammal" and "mammal-pet". How can I modify the query, to only get one of them, for example:
- pet-mammal
- insect-can fly