How can I select the top 10 largest numbers from a database column using SQL?
I have a database table which contains a column that records page hits for every entry.
I want to select the top 5 most hit pages from the database, but can't seem to find the right method to do so using ju开发者_如何学Pythonst SQL. In particular I'm looking for one which doesn't involve selecting every entry and scanning through it afterwards using PHP.
What's the best way to do this via SQL (if there is one)?
Thanks.
Try this approach:
SELECT column1, column2, hit_pages,...
FROM YourTable
ORDER BY hit_pages DESC
LIMIT 5
In MySQL> SELECT * FROM table ORDER BY hits DESC limit 5;
In Oralce> SELECT * FROM table ORDER BY hits DESC where rownum <5;
SELECT TOP 10 price(column Name) from products(tablename) order by price desc;
Below is the output
price 263.5 123.79 97 81 62.5 55 53 49.3 46 45.6
for find 20th maximum value primary key with where condition:
declare @max20Pk int=0;
select @maxPk=min(Pk_Transaction) from(
select top 20 Pk_Transaction from tblTransaction with(nolock)
where Fk_ChargeType in(2,3)
order by Pk_Transaction desc
) as dtt
精彩评论