![](/img/trans.png)
[英]MySQL query for showing parent child in a table with GROUP_CONCAT
[英]MySql: Group by parent then child
我有一个父子表结构,如:
CREATE TABLE t (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`),
`parent` int(10) unsigned DEFAULT NULL, INDEX parent(`parent`),
`attr` tinyint(1) NOT NULL DEFAULT '0'
);
INSERT INTO t SET id=1, parent=null, attr=1;
INSERT INTO t SET id=2, parent=1, attr=1;
INSERT INTO t SET id=3, parent=1, attr=1;
INSERT INTO t SET id=4, parent=null, attr=0;
INSERT INTO t SET id=5, parent=4, attr=1;
INSERT INTO t SET id=6, parent=4, attr=1;
INSERT INTO t SET id=7, parent=null, attr=1;
INSERT INTO t SET id=8, parent=null, attr=0;
我想查询“ attr = 1”。 结果应为:
如果找到父母,则输出父母而不是孩子
如果找到了孩子但没有找到父母,请显示找到的孩子
因此,对于上面的测试数据,结果应为ID 1、5、6、7。
我用类似的方式尝试过:
SELECT id, parent FROM t WHERE attr=1 GROUP BY COALESCE(parent, id);
但这只会找到ID 1、5、7,而不是ID 6。
这里4将不视为attr = 0
这只是一个简化的测试结构,因此,如果可以的话,可以避免连接或子选择,以保持查询的快速,那将是很好的选择。
您可能正在使用self-join
来寻找类似的东西
SELECT COALESCE(parent.id, child.id)
FROM t child
left join t parent on (parent.id = child.parent and parent.attr = 1 )
WHERE child.attr = 1
GROUP BY COALESCE(parent.id, child.id)
我尝试了以下方法,并且可以得到预期的结果。 您也可以考虑看看它。
select id, parent from t
where attr = 1
and (parent not in
(select id from t where parent is NULL and attr = 1)
or parent is NULL);
我创建了表并进行了测试,它返回了您描述的行:
SELECT
t1.id,
t1.parent
FROM t as t1
LEFT JOIN t as t2
ON t1.parent = t2.id
WHERE (COALESCE(t1.attr,0) = 1 OR COALESCE(t2.attr,0) = 1)
AND COALESCE(t2.attr,0) <> COALESCE(t1.attr,0)
select distinct
case when t.parent is not null and p.attr = 1 then t.parent
else t.id
end id
from t left join t p
on t.parent = p.id
where t.attr = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.