读书人

50求一SQL语句正解即结贴!解决方

发布时间: 2012-01-21 21:31:43 作者: rapoo

50求一SQL语句,正解即结贴!
news表
cid(文本) ctitle bigclassID infotime
20060406100016 asdf 20060315100001 2006-3-6 14:54:57
20060406100017 asdf1 20060315100001 2006-4-6 14:54:57
20060406100018 asdf2 20060315100001 2006-5-6 14:54:57
20060406100019 qwer 20060315100002 2006-6-6 14:54:57
20060406100020 awer 20060315100002 2006-6-814:54:57
20060406100021 sdf 20060315100003 2006-7-6 14:54:57
20060406100022 12asdf 20060315100001 2007-5-6 14:54:57
20060406100023 3qwer 20060315100002 2007-6-6 14:54:57
20060406100024 4awer 20060315100004 2007-7-6 14:54:57
20060406100025 4sdf 20060315100003 2007-8-6 14:54:57

bigclass表
bigclassID name
20060315100001 a
20060315100002 b
20060315100003 c
20060315100004 d

在access中,查询的结果为
20060406100022 12asdf 20060315100001 2007-5-6 14:54:57
20060406100018 asdf 20060315100001 2006-5-6 14:54:57
20060406100023 3qwer 20060315100002 2007-6-6 14:54:57
20060406100020 awer 20060315100002 2006-6-814:54:57
20060406100025 4sdf 20060315100003 2007-8-6 14:54:57
20060406100021 sdf 20060315100003 2006-7-6 14:54:57


20060406100024 4awer 20060315100004 2007-7-6 14:54:57

关键每类都有两条数据.也就是说每类最多只两条.如5类查询出来是最多10条,4类查询出来的最多是8条

[解决办法]
需要用吗?

如果需要用,关联一下就可以了。
[解决办法]
SELECT A.*,c.name --加C.name
FROM News as A,bigclass C --加bigclass C
where A.bigclassid=c.bigclassid and --加A.bigclassid=c.bigclassid and 连接条件
cid in (select top 2 cid from News where bigclassID=A.bigclassID order by cid des)
corder by a.bigclassid,a.cid desc--加排序
[解决办法]
wangtiecheng(不知不为过,不学就是错!)
正解

读书人网 >SQL Server

热点推荐