合并到具有相同 ID 或属性 SQL Group_concat 的一组没有帮助:(
Combine to one group with the same id or attribute SQL Group_concat doesn't help:(
如果它们具有相同的 ID 或相同的属性,我需要将行分组在一行中。所以我想我需要使用 INNER JOIN
和 GROUP_CONCAT
,但我不知道如何使用。问题是,如果两个用户没有共同属性,但与相同的第三个用户属于同一组,则必须将所有三个用户合并为一个组。此外,我在 table.
中没有 group_id
列
group_id, user_id, group_attributes
1, 1, "red, green, yellow, grey, purple, coffeemaker"
1, 2, "red, green, yellow, grey, purple, coffeemaker"
1, 3, "red, green, yellow, grey, purple, coffeemaker"
1, 4, "red, green, yellow, grey, purple, coffeemaker"
1, 5, "red, green, yellow, grey, purple, coffeemaker"
2, 6, "coffee, milk, croissant"
2, 7, "coffee, milk, croissant"
2, 8, "coffee, milk, croissant"
减少回答时间的原始数据。
CREATE TABLE task (
user_id INT(10) NOT NULL,
attribute VARCHAR(50) NULL DEFAULT NULL);
INSERT INTO task (user_id, attribute)
VALUES
(1, 'red'),
(1, 'green'),
(2, 'green'),
(2, 'yellow'),
(3, 'grey'),
(3, 'coffeemaker'),
(4, 'grey'),
(4, 'purple'),
(5, 'purple'),
(5, 'red'),
(6, 'black'),
(7, 'black'),
(7, 'milk'),
(8, 'milk'),
(8, 'croissant');
这是一个图行走问题,因此简单的 JOIN
是不够的。一种方法是获取与给定属性关联的所有属性。以下递归 CTE 执行此操作:
with recursive aa as (
select distinct t1.attribute as at1, t2.attribute as at2
from task t1 join
task t2
on t1.user_id = t2.user_id
),
cte as (
select at1, at2, at1 as found, 1 as lev
from aa
union all
select cte.at1, aa.at2, concat_ws(',', found, aa.at2), lev + 1
from cte join
aa
on cte.at2 = aa.at1
where find_in_set(aa.at2, found) = 0
)
select distinct at1, at2
from cte;
然后您可以使用相同的递归 CTE 将这些值组合成一个字符串:
with recursive aa as (
select distinct t1.attribute as at1, t2.attribute as at2
from task t1 join
task t2
on t1.user_id = t2.user_id
),
cte as (
select at1, at2, at1 as found, 1 as lev
from aa
union all
select cte.at1, aa.at2, concat_ws(',', found, aa.at2), lev + 1
from cte join
aa
on cte.at2 = aa.at1
where find_in_set(aa.at2, found) = 0
)
select dense_rank() over (order by pairs.all_attributes) as group_id, t.user_id, pairs.all_attributes
from (select at1, group_concat(at2) as all_attributes
from cte
group by at1
) pairs join
(select user_id, min(attribute) as min_attribute
from task
group by user_id
) t
on t.min_attribute = pairs.at1;
我看不出这段代码有什么问题。但是 db<>fiddle 坚持要为 pairs
创建一个十六进制字符串。但是,我认为这将适用于您的数据库。这是 fiddle.
如果它们具有相同的 ID 或相同的属性,我需要将行分组在一行中。所以我想我需要使用 INNER JOIN
和 GROUP_CONCAT
,但我不知道如何使用。问题是,如果两个用户没有共同属性,但与相同的第三个用户属于同一组,则必须将所有三个用户合并为一个组。此外,我在 table.
group_id
列
group_id, user_id, group_attributes
1, 1, "red, green, yellow, grey, purple, coffeemaker"
1, 2, "red, green, yellow, grey, purple, coffeemaker"
1, 3, "red, green, yellow, grey, purple, coffeemaker"
1, 4, "red, green, yellow, grey, purple, coffeemaker"
1, 5, "red, green, yellow, grey, purple, coffeemaker"
2, 6, "coffee, milk, croissant"
2, 7, "coffee, milk, croissant"
2, 8, "coffee, milk, croissant"
减少回答时间的原始数据。
CREATE TABLE task (
user_id INT(10) NOT NULL,
attribute VARCHAR(50) NULL DEFAULT NULL);
INSERT INTO task (user_id, attribute)
VALUES
(1, 'red'),
(1, 'green'),
(2, 'green'),
(2, 'yellow'),
(3, 'grey'),
(3, 'coffeemaker'),
(4, 'grey'),
(4, 'purple'),
(5, 'purple'),
(5, 'red'),
(6, 'black'),
(7, 'black'),
(7, 'milk'),
(8, 'milk'),
(8, 'croissant');
这是一个图行走问题,因此简单的 JOIN
是不够的。一种方法是获取与给定属性关联的所有属性。以下递归 CTE 执行此操作:
with recursive aa as (
select distinct t1.attribute as at1, t2.attribute as at2
from task t1 join
task t2
on t1.user_id = t2.user_id
),
cte as (
select at1, at2, at1 as found, 1 as lev
from aa
union all
select cte.at1, aa.at2, concat_ws(',', found, aa.at2), lev + 1
from cte join
aa
on cte.at2 = aa.at1
where find_in_set(aa.at2, found) = 0
)
select distinct at1, at2
from cte;
然后您可以使用相同的递归 CTE 将这些值组合成一个字符串:
with recursive aa as (
select distinct t1.attribute as at1, t2.attribute as at2
from task t1 join
task t2
on t1.user_id = t2.user_id
),
cte as (
select at1, at2, at1 as found, 1 as lev
from aa
union all
select cte.at1, aa.at2, concat_ws(',', found, aa.at2), lev + 1
from cte join
aa
on cte.at2 = aa.at1
where find_in_set(aa.at2, found) = 0
)
select dense_rank() over (order by pairs.all_attributes) as group_id, t.user_id, pairs.all_attributes
from (select at1, group_concat(at2) as all_attributes
from cte
group by at1
) pairs join
(select user_id, min(attribute) as min_attribute
from task
group by user_id
) t
on t.min_attribute = pairs.at1;
我看不出这段代码有什么问题。但是 db<>fiddle 坚持要为 pairs
创建一个十六进制字符串。但是,我认为这将适用于您的数据库。这是 fiddle.