有如下数据表与数据
通过递归查询到如下数据
select * from comments where find_in_set(pid,getChildLst(1));
我希望通过add_date和level来排序,得到如下结果
id pid level
6 1 1
3 1 1
7 3 2
2 1 1
4 2 2
5 4 3新手不懂,请大师们指教如何写select语句,或者有什么思路可实现。非常感谢。
通过递归查询到如下数据
select * from comments where find_in_set(pid,getChildLst(1));
我希望通过add_date和level来排序,得到如下结果
id pid level
6 1 1
3 1 1
7 3 2
2 1 1
4 2 2
5 4 3新手不懂,请大师们指教如何写select语句,或者有什么思路可实现。非常感谢。
第一行 6
第二行 3,7
第三行 2,4,5
感谢ACMAIN_CHM版主,但经查询,查询得到的结果如下:
getChildLst函数如下:DROP FUNCTION `getChildLst`//
CREATE DEFINER=`root`@`localhost` FUNCTION `getChildLst`(rootId INT) RETURNS varchar(1000) CHARSET latin1
Begin
Declare sTemp varchar(1000);
Declare sTempChd varchar(1000);
Set sTemp = '$';
Set sTempChd = cast(rootId as CHAR);
While sTempChd is not null Do
Set sTemp = concat(sTemp,',',sTempChd);
Select group_concat(id) into sTempChd from comments where find_in_set(pid,sTempChd)>0;
End While;
Return sTemp;
End这是网上的一段函数,菜鸟未能理解,请ACMAIN_CHM版主帮忙看一下如何改,非常感谢。
在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点。但很遗憾,在MySQL的目前版本中还没有对应的功能。 在MySQL中如果是有限的层次,比如我们事先如果可以确定这个树的最大深度是4, 那么所有节点为根的树的深度均不会超过4,则我们可以直接通过left join 来实现。 但很多时候我们...