我有这个mysql表"类别":
BigInteger bi = new BigInteger(1000);
我想得到这个结果:
id category parent
1 category1 NULL
2 subcategory1 1
3 category2 NULL
4 subcategory2 2
得到这个结果我使用这段代码,但速度很慢:
category1
subcategory1
subcategory2
category2
你有更快的解决方案吗? 感谢
答案 0 :(得分:0)
如果您知道树的深度(最大或期望),则可以将其全部放在SQL中,展平为表示路径的行:
SELECT c.category, c1.category AS sub1, c2.category AS sub2, c3.category AS sub3
FROM categories c
LEFT JOIN
(categories c1
LEFT JOIN
(categories c2
LEFT JOIN categories c3
ON c3.parent = c2.category)
ON c2.parent = c1.category)
ON c1.parent = c.category
WHERE c.parent IS NULL;
有了这个,相应地在UI中呈现它并不是什么大不了的事。
答案 1 :(得分:-1)
private List<PolyRegion2D> Joiner(IEnumerable<Polygon2D> polygons) {
// The resulting polygons are unconnected
List<PolyRegion2D> res = new List<PolyRegion2D>();
// Put every polygon in a region to do the unions.
LinkedList<PolyRegion2D> polygonRegions = new LinkedList<PolyRegion2D>();
foreach (Polygon2D polygon in polygons) {
polygonRegions.AddLast(new PolyRegion2D(new Polygon2D[]{polygon}));
}
while (polygonRegions.Count > 0) {
PolyRegion2D first = polygonRegions.First.Value;
polygonRegions.RemoveFirst();
PolyRegion2D union;
LinkedListNode<PolyRegion2D> connected = FindConnected(first, polygonRegions, out union);
if (connected == null) {
// Unconnected polygon
res.Add(first);
} else {
// Intersection found
polygonRegions.Remove(connected);
polygonRegions.AddFirst(union);
}
}
return res;
}
private LinkedListNode<PolyRegion2D> FindConnected(PolyRegion2D poly, LinkedList<PolyRegion2D> polys, out PolyRegion2D union) {
LinkedListNode<PolyRegion2D> node = polys.First;
while(node != null){
PolyRegion2D[] union_ = PolyRegion2D.Union(poly, node.Value);
if (union_.Length == 1) {
union = union_[0];
return node;
}
node = node.Next;
}
union = null;
return null;
}