mysql是可以使用递归函数的,如下例子:mysql树查询,sql递归函数selectcateidfromgoodscategorywherefind_in_set(cateid,getgoodschildlst('43242'))create。
- 情感问答
- 答案列表
mysql 树查询:mysql为什么用b 树[朗读]
很高兴回答你的问题根据你的需求,写的sql如下:selectid,lpad(name,length(name)+(level*10),'')fromtable_namewhere1=1startwithfidisnullconnectbypriorid=fid;根据你的问题补充:selectidfromtable_namewhereidnotin(selectidfromtable_namewhere1=1startwithid=1connectbypriorid=fid);上边这个sql就查询出来当id=1的时候,能作为它上级的id,希望对你有所帮助。
一般比较普遍的就是四种方法:(具体见sqlanti-patterns这本书)adjacencylist:每一条记录存parent_idpathenumerations:每一条记录存整个treepath经过的node枚举nestedsets:每一条记录存nleft和nrightclosuretable:维护一个表,所有的treepath作为记录进行保存。
根据你的需求,写的sql如下:selectid,lpad(name,length(name)+(level*10),'')fromtable_namewhere1=1startwithfidisnullconnectbypriorid=fid;根据你的问题补充:selectidfromtable_namewhereidnotin(selectidfromtable_namewhere1=1startwithid=1connectbypriorid=fid);上边这个sql就查询出来当id=1的时候,能作为它上级的id,希望对你有所帮助。
当然这种结构就不要追求什么效率了.如果要效率高的,只能改表结构.1:selectp2.whereb.pid>=2;3.通过程序或数据库的storeprocedure来实现了.在mysql中无法以?