读书人

主从表查询,该如何处理

发布时间: 2012-04-24 14:15:38 作者: rapoo

主从表查询
主表字段

SQL code
id inttitle varchar

从表字段
SQL code
id intparentid int  --主表的IDfilename 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 son p.id=s.parentid where filename is not null or filename<>'' 

读书人网 >SQL Server

热点推荐