简体   繁体   中英

How to sort Sql column by frequency and select the row

I'm wondering how I can sort an sql column by frequency, and then pick which row I want. I already know that you need to use:

SELECT       `column`
    FROM     `your_table`
    GROUP BY `column`
    ORDER BY COUNT(*) DESC
    LIMIT    1;

To sort from most frequent to least frequent. But how would I select the 1st, 2nd, or 3rd most occurring with using the least amount of different SQL? Could I use PHP?

Is there something akin to:

MAX(VAL)-2 

or something close to this, for the 3rd most frequent?

Thanks!

You would use the limit clause. For instance:

limit 1 offset 0

would get the first.

limit 1 offset 2

would get the third.

( offset starts counting at 0 rather than 1.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM