gpt4 book ai didi

mysql - 在 where 子句中使用带有 'union' 和 'in' 的 MySQL group_concat 函数

转载 作者:行者123 更新时间:2023-11-29 19:38:38 24 4
gpt4 key购买 nike

我在 MySQL 中正确执行 group_concat() 函数时遇到问题。我的数据库中有一些受操作影响的实体。某些操作需要处理详细信息,这些详细信息作为处理的 ID 提供。我的处理详细信息可能是某些主要处理的子项,在数据库中这表示为

parent_id

在我的查询中,我向数据库询问已由所有子处理处理过的所有实体,这些子处理与我的起始实体的处理具有相同的parent_id(我事先获得了包含所有信息的单个实体)。

在数据库中,where 子句如下所示:

where p.processing_id in
(SELECT processing_id FROM processing_type_1 where parent_id in
(select parent_id from processing_type_1 where parent_id in (p.processing_id)))
union all (SELECT processing_id FROM processing_type_2 where parent_id =
(select parent_id from processing_type_2 where parent_id in (p.processing_id)))

此子句按预期工作,直到我尝试在 select 子句中添加 group_concat() 为止。实体上的每个操作都分配有人员,我需要将所有人员分配给某些操作。

select 子句如下所示:

group_concat(distinct (select z.full_name from user z join entity_action ea on z.id = ea.personnel_id where ea.entity_id = e.id and ea.action_type = 'some_action' order by z.full_name) separator ', ') as action_personnel

这个查询在我添加之前不起作用

limit 1

位于 group_concat() 中子查询的末尾。如果只分配一个人执行该操作,那不会有问题,但不幸的是,情况并非如此。

我的问题是 - 有什么方法可以让这两个工作吗?

我的示例数据如下所示:

实体表

entity_id
1
2
3

实体 Action 表

action_id|entity_id|  action_name|personnel_id|processing_id
1| 1|'some action'| 1| 15
2| 1| 'other'| 1|
3| 1| 'another'| 2|
4| 1|'some action'| 3| 17

加工表

processing_id|parent_id
15| 5
17| 5

如果我询问parent_id为5的所有处理,在这种情况下我想要的结果将是

entity_id|action_personnel
1| 1,3

我的完整查询如下:

SELECT e.entity_id, 
group_concat(distinct (select z.full_name from user z join entity_action ea on z.id = ea.personnel_id where ea.entity_id = e.id and ea.action_type = 'some_action' order by z.full_name) separator ', ') as action_personnel
FROM enity e
inner join entity_action ea on ea.entity_id = e.entity_id
left outer join processing p on p.id = ea.entity_id
where
(1 IS null OR
p.processing_id in
(SELECT processing_id FROM processing_type_1 where parent_id in
(select parent_id from processing_type_1 where parent_id in (p.processing_id)))
union all (SELECT processing_id FROM processing_type_2 where parent_id =
(select parent_id from processing_type_2 where parent_id in (p.processing_id)))
)
group by e.entity_id;

最佳答案

现在对这个有更多的了解。

我认为你的基本查询可以消除一些像这样的 IN 子句(但这不起作用)

SELECT e.entity_id, 
GROUP_CONCAT((SELECT DISTINCT z.full_name FROM user z INNER JOIN entity_action ea ON z.id = ea.personnel_id WHERE ea.entity_id = e.id AND ea.action_type = 'some_action') ORDER BY full_name SEPARATOR ', ') AS action_personnel
FROM enity e
INNER JOIN entity_action ea ON ea.entity_id = e.entity_id
WHERE (1 IS NULL
OR p.processing_id in
(
SELECT processing_id
FROM processing_type_1 pt1_a
INNER JOIN processing_type_1 pt1_b
ON pt1_a.parent_id = pt1_b.parent_id
INNER processing p on p.id = ea.entity_id
ON pt1_b.parent_id = p.processing_id
UNION
SELECT processing_id pt2_a
FROM processing_type_2
INNER JOIN processing_type_2 pt2_b
ON pt2_a.parent_id = pt2_b.parent_id
INNER processing p on p.id = ea.entity_id
ON pt2_b.parent_id = p.processing_id
)
)
GROUP BY e.entity_id;

看看这个,您在 GROUP_CONCAT 内的 SELECT 中有一个子查询。以前从未见过有人尝试过此操作,并且手册中没有在 GROUP CONCAT 中提及任何有关此操作的内容。然而,当我使用这样的语法时,MySQL 似乎很困惑,因为它有一个子查询在 SELECT 中返回多行,这会导致错误。这就是为什么当您向子查询添加 LIMIT 1 时它会起作用,因为它强制它返回单行。

我清理后的查询也遇到同样的问题。

也许可以清理它,只在主查询中对用户和实体操作进行联接,但不确定您的数据。

您可以做的是加入子查询,以将每个entity_id的所有全名分组在一起:-

SELECT e.entity_id, 
sub1.action_personnel
FROM enity e
INNER JOIN entity_action ea ON ea.entity_id = e.entity_id
INNER JOIN
(
SELECT ea.entity_id,
GROUP_CONCAT(DISTINCT z.full_name ORDER BY full_name SEPARATOR ', ') AS action_personnel
FROM user z
INNER JOIN entity_action ea
ON z.id = ea.personnel_id
WHERE ea.entity_id = e.id
AND ea.action_type = 'some_action'
GROUP BY ea.entity_id
) sub1
ON sub1.entity_id = e.id
WHERE (1 IS NULL
OR p.processing_id in
(
SELECT processing_id
FROM processing_type_1 pt1_a
INNER JOIN processing_type_1 pt1_b
ON pt1_a.parent_id = pt1_b.parent_id
INNER processing p on p.id = ea.entity_id
ON pt1_b.parent_id = p.processing_id
UNION
SELECT processing_id pt2_a
FROM processing_type_2
INNER JOIN processing_type_2 pt2_b
ON pt2_a.parent_id = pt2_b.parent_id
INNER processing p on p.id = ea.entity_id
ON pt2_b.parent_id = p.processing_id
)
)
GROUP BY e.entity_id;

关于mysql - 在 where 子句中使用带有 'union' 和 'in' 的 MySQL group_concat 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com