34,593
社区成员
发帖
与我相关
我的任务
分享
id int
title varchar
id int
parentid int --主表的ID
filename varchar
SELECT distinct T1.ID,T1.TITLE,T2.parentid FROM
(SELECT TOP 25 A.ID,A.TITLE FROM temp_main a,temp_detail b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')) T1,temp_detail T2
WHERE T1.ID=T2.ID
/*
1 test1 1
2 test2 1
3 test3 2
4 test4 3
6 test6 4
*/
select top 25 p.id,p.title,s.filename from temp_main as p inner join temp_detail as s
on p.id=s.parentid
where filename is not null or filename<>''
SELECT T1.ID,T1.TITLE,T2.parentid
(SELECT TOP 25 ID,TITLE FROM ta a,tb b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')) T1,tb T2
WHERE T1.ID=T2.ID
create table temp_main
(
[id][int]IDENTITY (1, 1) NOT NULL ,
[title][varchar](30) null,
CONSTRAINT [PK_temp_main] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GO
create table temp_detail
(
[id][int]IDENTITY (1, 1) NOT NULL ,
[parentid][int]null,
[filename][varchar](30) null,
CONSTRAINT [PK_temp_detail] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GO
insert into temp_main(title)values('test1')
insert into temp_main(title)values('test2')
insert into temp_main(title)values('test3')
insert into temp_main(title)values('test4')
insert into temp_main(title)values('test5')
insert into temp_main(title)values('test6')
insert into temp_detail(parentid,filename)values(1,'file1')
insert into temp_detail(parentid,filename)values(1,'file2')
insert into temp_detail(parentid,filename)values(2,'file3')
insert into temp_detail(parentid,filename)values(3,'file4')
insert into temp_detail(parentid,filename)values(3,'file5')
insert into temp_detail(parentid,filename)values(4,'file6')
insert into temp_detail(parentid,filename)values(6,'file7')
insert into temp_detail(parentid,filename)values(6,'file8')
go
SELECT TOP 25 a.* FROM ta a,tb b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')
SELECT top 25 a.* FROM ta a,tb b WHERE a.id=b.parentid AND( b.filename is not null AND filename<>'')
select top 25 a.* from tb a,tb b where a.id=b.id and b.filename is not null
select top 25 a.* from ta a join tb b on a.id=b.parentid
where b.filename is not null or filename<>''
if object_id(N'#1') is not null drop table #1
--declare @colname varchar(40)
create table #1 (id int,title varchar(40),filename varchar(50))
insert into #1(id,title) select id,title from temp_main where id in(select distinct parentid from temp_detail)
update #1 set filename=(select top 1 filename from temp_detail where parentid=#1.id)
select * from #1
drop table #1
1 test1 1 file1
2 test2 1 file2--这个是有问题的
3 test3 2 file3
4 test4 3 file4
6 test6 4 file6
SELECT distinct T1.ID,T1.TITLE,T2.parentid,T2.filename FROM
(SELECT TOP 25 A.ID,A.TITLE FROM temp_main a,temp_detail b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')) T1,temp_detail T2
WHERE T1.ID=T2.ID
SQL code
SELECT distinct T1.ID,T1.TITLE,T2.parentid FROM
(SELECT TOP 25 A.ID,A.TITLE FROM temp_main a,temp_detail b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')) T1,temp_……