9

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
2
  • There's no need to concatenate this data. In fact, to a rough approximation, there is no problem in SQL for which CONCAT or GROUP_CONCAT need be part of the answer.
    – Strawberry
    Apr 29, 2015 at 11:23
  • Just add a requirement that the id of one result must be lower than the id of the other result.
    – Strawberry
    Apr 29, 2015 at 11:36

3 Answers 3

2

You can also rewrite your query as

SELECT  DISTINCT GREATEST(CONCAT_WS("-", x.name, c.name),CONCAT_WS("-", c.name, x.name)) col
FROM animal_2_category a2c1 
    JOIN animal_2_category a2c2 ON a2c1.animal_id = a2c2.animal_id
    JOIN categories c ON c.id = a2c2.`category_id`
    JOIN categories `x` ON a2c1.category_id = x.id
    WHERE x.name <> c.name

DEMO

1

One way to avoid duplicate pairs is to ensure your pairs are always created in the same order. We can do that by assembling your pair in an if statement, which effectively sorts the two values for us. Then we just grab the distinct rows.

select distinct if(c1.name > c2.name, concat_ws('-', c1.name, c2.name), concat_ws('-', c2.name, c1.name)) pairing
  from animal_2_category a1
    inner join animal_2_category a2
      on a1.animal_id = a2.animal_id
        and a1.category_id <> a2.category_id
    inner join categories c1
      on a1.category_id = c1.id
    inner join categories c2
      on a2.category_id = c2.id;

Alternatively, just be more selective in the join criteria, so they only join in a particular order. This avoids the if, but we still need to grab the distinct values.

 select distinct concat_ws('-', c1.name, c2.name) pairing
  from animal_2_category a1
    inner join animal_2_category a2
      on a1.animal_id = a2.animal_id
        and a1.category_id < a2.category_id
    inner join categories c1
      on a1.category_id = c1.id
    inner join categories c2
      on a2.category_id = c2.id;

demo here

Either way, the results are the same.

1
SELECT DISTINCT combos
FROM (
  SELECT animal_id
  , GROUP_CONCAT(c.name ORDER BY c.name SERPERATOR '-') AS combos  
  FROM animal_2_category a2c
  JOIN category c ON c.id = a2c.category_id 
  GROUP BY animal_id
) AS ilv
1
  • this is good if he wants single categories to appear as well as category pairings, otherwise it needs some having love.
    – pala_
    Apr 29, 2015 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.