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

[一句不算太难的sql语句,麻烦了。 ]续贴
原贴http://community.csdn.net/Expert/topic/5443/5443301.xml?temp=.172497

paoluo(一天到晚游泳的鱼)
try
select   membername   from   member   where   memberid   in(select   top   10     memberid   from   info   Group   By   memberid   order   by   Max(infoid)   desc)
得到wszhoho

这个只能得到member表中第一个membername(因为member中wszhoho是排在其他的前面),读完这个,游标就下不来了。但是子句能得到info表中正确的排序结果:
wszhoho10
wszhoho7
wszhoho6
wszhoho8
wszhoho9
wszhoho5
wszhoho4
wszhoho3
wszhoho
wszhoho2

in关键字好象在这里没有作用了。

------解决方案--------------------
勇士 69-66 火箭
------解决方案--------------------
我特意測試了下,沒有問題啊。

Create Table member
(memberid Varchar(10),
membername Varchar(10))
Insert member Select 'wszhoho ', 'A1 '
Union All Select 'wszhoho2 ', 'A2 '
Union All Select 'wszhoho3 ', 'A3 '
Union All Select 'wszhoho4 ', 'A4 '
Union All Select 'wszhoho5 ', 'A5 '
Union All Select 'wszhoho6 ', 'A6 '
Union All Select 'wszhoho7 ', 'A7 '
Union All Select 'wszhoho8 ', 'A8 '
Union All Select 'wszhoho9 ', 'A9 '
Union All Select 'wszhoho10 ', 'A10 '
Union All Select 'wszhoho11 ', 'A11 '
Union All Select 'wszhoho12 ', 'A12 '

Create Table info
(infoid Int,
memberid Varchar(10))

Insert info Select 1, 'wszhoho2 '
Union All Select 2, 'wszhoho2 '
Union All Select 3, 'wszhoho '
Union All Select 4, 'wszhoho3 '
Union All Select 5, 'wszhoho4 '
Union All Select 6, 'wszhoho5 '
Union All Select 7, 'wszhoho9 '
Union All Select 8, 'wszhoho8 '
Union All Select 9, 'wszhoho6 '
Union All Select 10, 'wszhoho7 '
Union All Select 11, 'wszhoho10 '
GO
select membername from member where memberid in(select top 10 memberid from info Group By memberid order by Max(infoid) desc)
Go
Drop Table member, info
--Result
/*
membername
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
*/
------解决方案--------------------
Hi,paoluo(一天到晚游泳的鱼)
游到这里来啦?