日期:2014-05-16  浏览次数:20687 次

Mysql Sql查询,各位大大进来看下
Id ParintId Name
1 0 中国移动
2 1 广东移动
3 2 深圳移动
4 3 南山移动
5 4 科技园移动
..........
就是这样的无穷级别的父子ID匹配关系
现需要用SQL查询出来所以的子类,比如我给一个广东移动的ID为2 需要找出所有在广东移动下去的子移动,注意是无穷级别的。

------解决方案--------------------
http://blog.csdn.net/acmain_chm/article/details/4142971
MySQL中进行树状所有子节点的查询
在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点。但很遗憾,在MySQL的目前版本中还没有对应的功能。 在MySQL中如果是有限的层次,比如我们事先如果可以确定这个树的最大深度是4, 那么所有节点为根的树的深度均不会超过4,则我们可以直接通过left join 来实现。 但很多时候我们...
------解决方案--------------------
多加一张存放 子移动 的表,通过相同的id用外联进行查询,如:
Id ParintId Name
1 2 广东区1
2 2 广东区2
3 2 广东区3
4 2 广东区4
5 2 广东区5
------解决方案--------------------
这个需要写个存储过程

create procedure sp_test(id1 int)
begin
create TEMPORARY table temp(id int);
insert insert temp values(id1);
declare id2 int;
declare id3 int;
set id2 = id1;
while exists (select 1 from tbname where parintid=id2) do
select id into id3 from tbname where parintid=id2;
insert into temp values(id3);
set id2 = id3;
en while
select * from tbname where id in (select id from temp);
end