在SQL中,TOP N用于限制查询结果集的行数
DECLARE @top_n INT = 10;
SELECT TOP (@top_n) * FROM your_table ORDER BY some_column DESC;
SELECT TOP (CASE WHEN condition THEN value1 ELSE value2 END) * FROM your_table ORDER BY some_column DESC;
SELECT TOP (SELECT COUNT(*) FROM another_table WHERE some_condition) * FROM your_table ORDER BY some_column DESC;
SELECT TOP (10) WITH TIES * FROM your_table ORDER BY some_column DESC;
SELECT * FROM your_table ORDER BY some_column DESC OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY some_column DESC) AS row_num FROM your_table) AS ranked_table WHERE row_num <= 10;
请注意,这些技巧可能因数据库类型而异。在实际应用中,请根据你所使用的数据库系统进行调整。