域名城

查看: 1026|回复: 1
打印 上一主题 下一主题

SQL递归查询实现跟帖盖楼效果

[复制链接]

米农

Rank: 2

10

主题

10

帖子

32

城市币
ID
543338
积分
132
实名认证
未实名
注册时间
2016-11-14
最后登录
2016-11-29
跳转到指定楼层
1#
发表于 2016-11-19 13:15 | 只看该作者 |倒序浏览
网易新闻的盖楼乐趣多,某一天也想实现诸如网易新闻跟帖盖楼的功能,无奈技术不佳(基础不牢),网上搜索了资料才发现SQL查询方法有一种叫递归查询,整理如下:
一、查询出 id = 1 的所有子结点
with my1 as(select * from table where id = 1  union all select table.* from my1, table where my1.id = table.fatherId) select * from my1   
结果包含1这条记录,如果不想包含,可以在最后加上:where id <> 1
二、查询出 id = 2 的所有父结点
with my1 as(select * from table where id = 2  union all select table.* from my1, table where my1.fatherId = table.id ) select * from my1;  
三、删除 id = 1 的所有子结点(包括id = 1结点)
with my1 as(select * from table where id = 1  union all select table.* from my1, table where my1.id = table.fatherId ) delete from table where exists (select id from my1 where my1.id = table.id)
本文摘自木庄网络博客>>SQL递归查询实现跟帖盖楼效果 http://www.muzhuangnet.com/show/105.html

米农

Rank: 2

0

主题

171

帖子

467

城市币
ID
542904
积分
1067
实名认证
未实名
注册时间
2016-10-28
最后登录
2017-1-13
2#
发表于 2016-11-20 10:34 | 只看该作者
很好,,,
www.rg96899.com
www.11nsq.com
www.55nsq.com
www.88nsv.com
www.99nsv.com
www.00nnsb.com
www.00sbcc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

易介集团旗下品牌 - 域名城| 国域| 中付通| 中介通| 中华知识产权网| 商标城| DoName| 域名论坛

电信与信息服务业务经营许可证 京ICP证090790号  京ICP备10003495号  电信业务审批[2010]字第598号函  京公网安备110108903585号

Archiver|手机版|小黑屋|侵权投诉|Club.domain.cn 中国域名论坛 论坛管理员邮箱:club@domain.cn

Powered by Discuz!X3.2GMT+8, 2024-9-20 22:34 © 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表