表名: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请大家帮忙看看是哪里写错了。谢谢!
字段
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请大家帮忙看看是哪里写错了。谢谢!
這個只會得到1條數據,你的結果怎麼有6條數據?
{
com.CommandText = "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";
TopId += 1;
}这不就6条了啊
select top 6 id from keyword where nsid='11' and directory='1' order by count desc這樣不就是你的結果了?