select top 4 a.Name as Name,b.Content as Content,b.ID as ID,b.Img as Img,a.ID from CultureType as a,BuddhistCulture as b where a.ID = b.TypeID and b.ID = (select top 1 c.ID from BuddhistCulture as c where a.ID = c.TypeID order by c.ID Desc)
上面这条sql语句 查出来的结果是
求大神帮忙写个sql 查出表里的第三条和第四条数据
上面的语句再套一层,按照你现在的顺序倒序,取top 2
可以帮我写写吗。。。。 懂道理 不会写
@Ashe:
select top 2 * from (
select top 4 a.Name as Name,b.Content as Content,b.ID as BID,b.Img as Img,a.aID from CultureType as a,BuddhistCulture as b where a.ID = b.TypeID and b.ID = (select top 1 c.ID from BuddhistCulture as c where a.ID = c.TypeID order by c.ID Desc)
) c
order by c.BID desc
如果不对,就把最后的排序改成c.aid desc
@我是大菠萝: c.aid desc???不对吧
@Ashe: 没有查出任何东西
top 4 倒叙 然后 top 2
你可以使用ROW_NUMBER()函数
其实你这基本写出来了,只是你问的没写为什么需要第三、四条数据,满足什么条件。
表数据如下:
1 11 11
2 22 22
3 33 33
4 44 44
5 55 55
6 66 66
sql语句:
select top 2 * from
(
select top 4 * from Test
order by id asc
) as t
order by t.id desc
楼主该结贴给分啦