大家好! 我的表里面有 几条记录,我想把“版本1”的记录以树形方式查询回来展示(以connect by 查询展示树形结构)但是发现2种查询方法有其一有重复记录 ,不知道为什么? 水果是夫类,苹果和桔子是他的子类数据如下:
列:
id,name,parentid,level,edition
1 水果 0 1 版本1
2 水果 0 1 版本2
3 苹果 1 2 版本1
4 苹果 2 2 版本2
5 桔子 1 2 版本1
6 桔子 2 2 版本2有问题的SQL:
select * from table where edition=1 connect by id=parentid start with parentid=0上面的sql查询回来有重复记录,每条记录重复2遍
下面这个没问题,只是括号位置变了:select * from (select * from table where edition=1) connect by id=parentid start with parentid=0
sql大概是上面这样,为什么第二种没有重复数据呢? 都是加了where edition=1的条件啊!
列:
id,name,parentid,level,edition
1 水果 0 1 版本1
2 水果 0 1 版本2
3 苹果 1 2 版本1
4 苹果 2 2 版本2
5 桔子 1 2 版本1
6 桔子 2 2 版本2有问题的SQL:
select * from table where edition=1 connect by id=parentid start with parentid=0上面的sql查询回来有重复记录,每条记录重复2遍
下面这个没问题,只是括号位置变了:select * from (select * from table where edition=1) connect by id=parentid start with parentid=0
sql大概是上面这样,为什么第二种没有重复数据呢? 都是加了where edition=1的条件啊!
第二个是在connect by之前使用where
第二个句子先使用where对需要递归的数据进行限制,再只对edition=1的数据进行递归connect by,所以不会出现重复记录。
多谢,“然后再取where除去了edition=2的记录(即id=4的记录),所以id=3的数据会出现两条
”既然去除了id=4的重复记录,为什么id=3的还有2条呢?