读书人

SQL语句Select出现有关问题请大家进

发布时间: 2012-02-04 15:43:08 作者: rapoo

SQL语句Select出现问题,请大家进来帮忙看看!
表名:keyword
字段
id 主键 标识
nsid nvarchar(50)
directory nvarchar(50)
count int

select top 1 id from keyword where id not in(select top " + TopId + " id from keyword where nsid= '11 ' and directory= '1 ') and nsid= '11 ' and directory= '1 '

这句是我要的结果,查询出来是完全正确的。
现在我想加个条件就是 order by count desc

然后我就改成:
select top 1 id from keyword where id not in(select top " + TopId + " id from keyword where nsid= '11 ' and directory= '1 ' order by count desc) and nsid= '11 ' and directory= '1 ' order by count desc

这样查询后如果count的值都不一样就没问题,如果count的值一样就会重复查询上次一样的id

比如有数据:
id count
54 1
55 1
56 1
57 1
58 3
59 0

正确的排序查询出来应该是
id count
58 3
54 1
55 1
56 1
57 1
59 0

可是现在竟然查出来竟然是

id count
58 3
54 1
54 1
54 1
54 1
59 0

请大家帮忙看看是哪里写错了。谢谢!

[解决办法]
select top 1 id from keyword where id not in(select top " + TopId + " id from keyword where nsid= '11 ' and directory= '1 ' order by count desc) and nsid= '11 ' and directory= '1 ' order by count desc

-------------
只得到1,你的果怎有6?
[解决办法]
narchar 也不+N ...
[解决办法]
dyjqk(http://www.news24h.cn), 你代不全的,光那一句,然只有1。

而且你循得到的是你第一列的果,那果是正常的。


[解决办法]

select top 6 id from keyword where nsid= '11 ' and directory= '1 ' order by count desc

不就是你的果了?
[解决办法]
再加上一排序字段下

select top 1 id from keyword where id not in(select top " + TopId + " id from keyword where nsid= '11 ' and directory= '1 ' order by count desc, id) and nsid= '11 ' and directory= '1 ' order by count desc, id

读书人网 >SQL Server

热点推荐