- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
自从我的问题here无法自信地回答,我在这里再次询问,希望有人确切知道: 指向 union 的指针和包含指向其元素的指针的 union 之间有什么区别(除了语法之外)吗? this中生成的程序集示例是
在 C 语言中,是否可以在另一个 union 体中定义一个 union 体?如果不是,为什么不可能?或者如果可以,可以在哪里使用? 最佳答案 假设您要定义: union myun { int x;
在 C 中,是否可以在另一个 union 中定义一个 union ?如果不是,为什么不可能?或者如果是,它可以在哪里使用? 最佳答案 假设你想定义: union myun { int x; s
我正在阅读一些代码并发现如下内容: typedef union { int int32; int boolean; time_t date; char *string;
我正在学习Lua,我更愿意使用冒号(:)作为方法。不幸的是,它并非在所有地方都有效。看我的代码: 设置= {} 本地mt = {} 函数Set:new(m) 本地集= {} setmetatable(
我遇到了一些性能问题,我有如下查询: SELECT * FROM Foo UNION SELECT * FROM Boo UNION SELECT * FROM Koo 我确信 Koo 不会返回任何重
This question already has answers here: C++ Structure Initialization (16个答案) 上个月关闭。 我正在尝试将一些用于嵌入式目标的
UNION 和 UNION ALL 有什么区别? 最佳答案 UNION 删除重复记录(结果中的所有列都相同),UNION ALL 则不会。 使用 UNION 而不是 UNION ALL 时会影响性能,
我想在两个表上使用联合运算符。我希望结果集消除由联合创建的重复值,但不消除表中预先存在的重复值。考虑这段代码... select b from (values (1), (2), (2
我知道 UNION 会删除重复项,但即使没有重复项也会更改结果顺序。 我有两个 select 语句,任何地方都没有 order by 语句 我想将它们合并或不合并(全部) 即 SELECT A UNI
基本上,我有一个 struct foo { /* variable denoting active member of union */ enum whichmembe
我有一个大规模查询,用于对许多表(每个表有数千行)执行 UNION ALL,然后在返回之前输出到临时表。 旧形式: SELECT * FROM (SELECT `a` AS `Human rea
UNION 和 UNION ALL 有什么区别? 最佳答案 UNION 删除重复记录(结果中的所有列都相同),UNION ALL 则不会。 使用 UNION 而不是 UNION ALL 时会影响性能,
如果我有两个 union 行结构: struct A { A() {} ~A() {} union { vector vi; vector db
考虑下面的代码,我已经写了: #include #include union myAccess { uint16_t access16; struct { uint
我想弄清楚你从 C99 中对齐变量的地役权中得到了什么: Exception to strict aliasing rule in C from 6.5.2.3 Structure and union
我正在通过 UNION 或 UNION ALL 从多个表中选择一列外键。 当重复无关紧要时,通常建议使用 UNION ALL 而不是 UNION 来解决性能问题。但是,在我的调用 PHP 脚本中,循环
在 C++ 中,union 可以包含静态成员,在类的情况下,这些成员属于一个类,因此对所有对象都是通用的。 union U { long l; int i; static long
任何人都可以提及普通和匿名 union (或结构)之间的区别吗?我刚找到一个: 不能在匿名 union 中定义函数。 最佳答案 您不需要点运算符“.”访问匿名 union 元素。 #include
我可能把这个复杂化了.. 我正在尝试在 Arduino 上用 C 语言为嵌入式应用程序制作一个相当可重用的分层菜单系统。我有结构来表示不同类型的菜单项,包括那些子菜单,以及这些菜单项的 union 是
我是一名优秀的程序员,十分优秀!