求教个SQL语句写法,关于去top10的选择

2025-03-02 12:27:32
推荐回答(5个)
回答1:

select top 10 A from table order by A desc
将A列从大到小排序,取出前十行数据
select * from table where A in (
select top 10 A from table order by A desc
)
将所有A列数值等于取出最大十个数值的数据全部查找出来

select top 10 A from (select distinct A from table )T order by A desc
将A列数值去除重复值,然后从大到小排序,取出不重复的前十行数据
select * from table where A in (
select top 10 A from (select distinct A from table )T order by A desc
)
将所有A列数值等于取出最大十个数值的数据全部查找出来

回答2:

换一个角度去理解问题可以吗?
你查找的排序好的最大值,就把它当做id处理好了,
查找方式就是与大于或等于top 10 里面的数值都找出
select tb_score.score from tb_score where //查询分数,从表tb_score
tb_score.score //条件--查询的分数不在某个区域里面
not in(
select tb_score.score from tb_score where //查询分数,从某个区域中
tb_score.score (select top 10 tb_score.score from tb_score //取前10个分数进行查询
order by score desc)
)
order by tb_score.score desc
步骤就是:先取出前10个分数(top 10),然后得出小于前10的有哪些(

回答3:

楼主说的应该是SQL Server的写法吧?
SQL Server TOP子句提供了ties关键字可以实现你的需要
SELECT TOP 10 WITH TIES * FROM table1 ORDER BY A DESC

回答4:

select * from table where A in (select top 10 A from table order by A)

回答5:

select top 10 a from table order by a desc