在where

时间:2017-01-03 08:50:33

标签: mysql group-concat union-all where-in

我在MySQL中正确执行group_concat()函数时遇到问题。我的数据库中有实体,这些实体是行动的主体。某些操作需要处理细节,这些细节作为处理的ID提供。我的处理细节可能是某些主要处理的子代,而在db中,这表示为

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_table

entity_id
1
2
3

entity_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;

2 个答案:

答案 0 :(得分:0)

尝试以下查询:

SELECT e.entity_id, 
GROUP_CONCAT(DISTINCT z.full_name 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
LEFT JOIN `user` z ON z.id = ea.personnel_id AND ea.action_type = 'some_action' 
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;

答案 1 :(得分:0)

现在更多地看一下。

我认为你的基本查询可以消除这样的一些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;