有两种可能的解释。从最近的评论中我知道你需要第一个:
排除被排除父母的孩子
因此,即使孩子不是编辑,如果他们的祖先之一是编辑,他们也应该被排除在外。这意味着您应该排除最内部查询中的记录:添加where
there:
select id,
name,
parent_id,
user_type
from (select * from p
where user_type <> 'editor'
order by parent_id, id) products_sorted,
(select @pv := '19') initialisation
where find_in_set(parent_id, @pv)
and length(@pv := concat(@pv, ',', id))
包括被排除在外的父母的孩子
在此解释中,您希望包含编辑器子代,无论是否要排除其任何祖先。
Add the user_type
领域中的select
列出,然后包装执行过滤器的查询,如下所示:
select *
from (
select id,
name,
parent_id,
user_type
from (select * from p
order by parent_id, id) products_sorted,
(select @pv := '19') initialisation
where find_in_set(parent_id, @pv)
and length(@pv := concat(@pv, ',', id))
) as sub
where user_type <> 'editor'
同样,这里的结果还将包括父层次结构(父母、祖父母、曾祖父母等)可能未完全包含的记录(因为其中一些可能是编辑者)。