源表-类别树
id, parent_id, state, name
我们有
id = 4, parent_id = 0, state = 1, name = SubCategory 0 (root)
id = 8, parent_id = 4, state = 0, name = SubCategory 1
id = 15, parent_id = 8, state = 1, name = SubCategory 2
id = 36, parent_id = 15, state = 1, name = SubCategory 3
作为检查所有父母状态的单个请求,如果至少一个状态= 0,则样本结果应为0行
尝试过递归
WITH RECURSIVE cte (id, state, parent_id, name) AS (
SELECT id,
state,
parent_id,
name
FROM t_pages
WHERE id = 36
UNION ALL
SELECT p.id,
p.state,
p.parent_id,
p.name
FROM t_pages p
INNER JOIN cte
ON p.id = cte.parent_id
AND p.state = 1
)
SELECT * FROM cte;
但是我不认为如何正确设置条件。在以上版本中,总是1选项给出(这是合乎逻辑的) 好吧,这太慢了-每张表有200个条目需要0.5秒
还有其他选择吗?
UPD:提琴https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=ecdbd0252a87aed8d3c6f5b3a31e0297