自连接
Select * from tableName a
join tableName b on ...
join tableName c on ...
Select * from tableName a
join tableName b on ...
join tableName c on ...
解决方案 »
- 求一个查询SQL执行时间的语句
- 存储过程中用IF语句出错
- 当内容是date时,日期范围可 between '1980/1/1' and '2050/1/1',就基本囊括所有日期;但是nvarchar时,如何能囊括所有内容?即between an
- 求指点:在Sql2005里用Clr存储过程替代Like检索,性能可以提高吗?
- MSSQL 2000 运行极慢
- sql 2005 设备折旧列出设备 计算精度不准确
- 求救
- SQL与内存
- (100分)关于利用Sql2000分析服务的多维数据集做统计指标的同期比较!高手指教
- vb中sql语名的输写????
- 关于Sql Server之间的联接
- 怎么把数据表中按右键,选“设计表”后,其中的内容导出?
declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
insert @a values(1,0,'中国')
insert @a values(2,0,'美国')
insert @a values(3,0,'加拿大')
insert @a values(4,1,'北京')
insert @a values(5,1,'上海')
insert @a values(6,1,'江苏')
insert @a values(7,6,'苏州')
insert @a values(8,7,'常熟')
insert @a values(9,6,'南京')
insert @a values(10,6,'无锡')
insert @a values(11,2,'纽约')
insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200),lev int)
insert @tmp1 select *,1 from @a where tc_ID=1
while exists(select 1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1))
insert @tmp1 select a.*,1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
select * from @tmp1/*--按子找父--*/
--建立环境
declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
insert @a values(1,0,'中国')
insert @a values(2,0,'美国')
insert @a values(3,0,'加拿大')
insert @a values(4,1,'北京')
insert @a values(5,1,'上海')
insert @a values(6,1,'江苏')
insert @a values(7,6,'苏州')
insert @a values(8,7,'常熟')
insert @a values(9,6,'南京')
insert @a values(10,6,'无锡')
insert @a values(11,2,'纽约')
insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200))--开始结点
insert @tmp1 select * from @a where tc_ID=10--循环得到
while exists(select 1 from @a a,@tmp1 b where a.tc_id=b.tc_pID and a.tc_ID not in (select tc_ID from @tmp1))
insert @tmp1 select a.* from @a a,@tmp1 b where a.tc_id=b.tc_pID and a.tc_ID not in (select tc_ID from @tmp1)--显示结果
select * from @tmp1/*--遍厉所有--*/
declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
insert @a values(1,0,'中国')
insert @a values(2,0,'美国')
insert @a values(3,0,'加拿大')
insert @a values(4,1,'北京')
insert @a values(5,1,'上海')
insert @a values(6,1,'江苏')
insert @a values(7,6,'苏州')
insert @a values(8,7,'常熟')
insert @a values(9,6,'南京')
insert @a values(10,6,'无锡')
insert @a values(11,2,'纽约')
insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200))
while exists(select 1 from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1))
begin
insert @tmp1 select top 1 * from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1) order by TC_Id
while exists(select 1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1))
insert @tmp1 select a.* from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
end
select * from @tmp1
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.6767542