日期:2014-05-19  浏览次数:20467 次

一个不好解决的sql语句的问题!
我想在一个表内查询返回两个字段的值,另外一个表根据这两个值中的其中一个字段的值来查询,要怎么办!

select   top   5   SectionID,Hits   from   BBS_Refers   order   by   Hits   desc
如我这里先获得SectionID和他的点击数的前5个数据,然后我在另外一个表想根据这个SectionID查询对应的字段,,要怎么办?
我是这样写的
select   *   from   BBS_Sections   where   SectionID   in(select   top   5   SectionID   from   BBS_Refers   order   by   Hits   desc)
但我后面这个select语句我想一起返回Hits字段的值,要怎么办?也就是形如
select   *   from   BBS_Sections   where   SectionID   in(select   top   5   SectionID,Hits   from   BBS_Refers   order   by   Hits   desc)
这样的,但这样语句肯定是不对的,我该怎么办?


------解决方案--------------------
select BBS_Sections.*, BBS_Refers.Hits
from BBS_Sections inner join BBS_Refers
on BBS_Sections.SectionID = SectionID
where SectionID in(select top 5 SectionID from BBS_Refers order by Hits desc)

------解决方案--------------------
回雪大哥!我原本语句是这样的
select top 5 ( 'BBS_ReferList.aspx?SectionId= '+ CAST(SectionID as varchar(14))) as SectionID,SectionName from dbo.BBS_Sections where SectionID=(select top 5 SectionID,Hits from BBS_Refers order by Hits desc)
替换后面的where为你的该怎么换?