Select TOP X (or bottom) percent for numeric values in MySQL

前端 未结 3 578
时光取名叫无心
时光取名叫无心 2020-11-29 10:15

I was wondering if there are any functions that can be used in MySQL to select the TOP X(or bottom) percent from a column containing numeric values.

Basically, I ha

3条回答
  •  执笔经年
    2020-11-29 10:50

    just as an FYI (i know this question is a few years old), this can be done other, cleaner ways as well.

    SELECT * FROM product_table WHERE price >= (SELECT price FROM product_table 
    ORDER BY price DESC LIMIT 1 OFFSET (SELECT 0.1 * COUNT(*) FROM product_table));
    

    i ran a similar query over a very large database, and it ran very quickly.

提交回复
热议问题