简体   繁体   English

Mysql排序合并的值

[英]Mysql sorting concated values

I have 3 mysql tables (animal, category and animal_2_category). 我有3个mysql表(动物,类别和animal_2_category)。 Every animal can have 0-n categories (for example fish, pet, insect, carnivore, "can fly", etc.). 每只动物都可以有0-n类别(例如鱼类,宠物,昆虫,食肉动物,“可以飞行”等)。

Table "animal" (id, name) 表“动物”(id,name)

1, dog
2, cat
3, bee
...

Table "categories" (id, name) 表“类别”(id,name)

1, pet
2, insect
3, mammal
4, fish
5, can fly
...

Table "animal_2_category" (animal_id, category_id) 表“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 昆虫可以飞

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

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. 我们可以通过在if语句中组合你的对来做到这一点,它有效地为我们排序这两个值。 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. 这避免了if,但我们仍然需要获取不同的值。

 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. 无论哪种方式,结果都是一样的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM