当前位置: 代码迷 >> Sql Server >> 主从表查询,该如何处理
  详细解决方案

主从表查询,该如何处理

热度:13   发布时间:2016-04-27 13:26:59.0
主从表查询
主表字段
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<>''
  相关解决方案