mysql 实现 start with
Posted ..SunXin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 实现 start with相关的知识,希望对你有一定的参考价值。
@param ids 要查询的起始 start with
* @param allres 包含要递归数据的结果集 ( 查询时别名ID PID )
* @param pos prior---> UP or DOWN
* @return
*/
public static List<Map<String, Object>> getTree(ArrayList<String> ids,
List<Map<String, Object>> allres,String pos) {
List<Map<String, Object>> res=new ArrayList<Map<String,Object>>();
if("up".equals(pos)){
res=toCreatTreeUp(ids,allres,res);
}
if("down".equals(pos)){
res=toCreatTreeDown(ids,allres,res);
}
return res;
}
private static List<Map<String, Object>> toCreatTreeUp(ArrayList<String> ids,
List<Map<String, Object>> allres,List<Map<String, Object>> res) {
ArrayList<String> idss = new ArrayList<String>();
for(String id :ids){
for (Map<String, Object> map : allres) {
if(id.equals(map.get("ID").toString())){
idss.add(map.get("PID").toString());
res.add(map);
}
}
}
if (idss.size()!=0) {
ids = idss;
res = toCreatTreeUp(ids,allres,res);
}
return res ;
}
private static List<Map<String, Object>> toCreatTreeDown(ArrayList<String> ids,
List<Map<String, Object>> allres,List<Map<String, Object>> res) {
ArrayList<String> idss = new ArrayList<String>();
for(String id :ids){
for (Map<String, Object> map : allres) {
if(id.equals(map.get("ID").toString())){
res.add(map);
}
if(id.equals(map.get("PID").toString())){
idss.add(map.get("ID").toString());
}
}
}
if (idss.size()!=0) {
ids = idss;
res = toCreatTreeDown(ids,allres,res);
}
return res ;
}
以上是关于mysql 实现 start with的主要内容,如果未能解决你的问题,请参考以下文章