在SQL中,TOP N用于限制查詢結果集的行數(shù)
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;
請注意,這些技巧可能因數(shù)據(jù)庫類型而異。在實際應用中,請根據(jù)你所使用的數(shù)據(jù)庫系統(tǒng)進行調(diào)整。