select up,name,downcount= (select count(*) from 表 a where a.up = b.down) from 表 b
给你一个思路,先建一个用户函数CREATE function getstr(@id int) returns int(4) as begin declare @str int set @str=0 select @str=@str+1 from b where up=@id return @str endselect a.*,dbo.getstr(a.rec_id) as downcount from a
(select count(*) from 表 a where a.up = b.down)
from 表 b
returns int(4)
as
begin
declare @str int
set @str=0
select @str=@str+1 from b
where up=@id
return @str
endselect a.*,dbo.getstr(a.rec_id) as downcount from a
不过如果一个表查询
可以
select a.*,b.* from 表 a,表 b where .......
字段1.Up,2.name,3.down,这个表是个树
除了根节点外,都有up值,除了叶节点外都有down值
我任意给一个up值(节点),我除了取出这个节点的up,name外,我还要知道他的叶有多少个,就是 节点.down=叶.up
多层且down有规律能体现顶层,把 Chiff(~o~) 修改一下就行,否则要用存储过程.