日期:2014-05-18  浏览次数:20329 次

主从表查询
主表字段
SQL code

id int
title varchar


从表字段
SQL code

id int
parentid int  --主表的ID
filename varchar



简单的1对n的关系,要求查出主表中的25条,filename不为空的记录;从表的记录没有约定,只要一条filename不为空即满足条件.


------解决方案--------------------
SQL code
select top 25 a.* from ta a join tb b on a.id=b.parentid 
where b.filename is not null or filename<>''

------解决方案--------------------
SQL code
select top 25 a.* from tb a,tb b where a.id=b.id and b.filename is not null

------解决方案--------------------
SQL code
SELECT  TOP 25 a.* FROM  ta a,tb b WHERE a.id=b.parentid AND( b.filename is not null AND b.filename<>'')

------解决方案--------------------
SQL code

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<>''