This question already has answers here:
Convert SQL Server query to MySQL [duplicate]
(1个答案)
Select TOP X (or bottom) percent for numeric values in MySQL
(3个答案)
去年关门了。
我需要返回select中所有记录的33%
select id , amount, 'low'
from table 1
where amount > 1
order by 2;

所以我需要把前33%的记录

最佳答案

我认为您需要枚举行,除了最新版本的MySQL之外,所有行都需要变量:

select t.*
from (select t.*, (@rn := @rn + 1) as rn
      from (select id , amount, 'low' as col
            from table 1
            where amount > 1
            order by 2
           ) t cross join
           (select @rn := 0) params
     ) t
where rn <= 0.33 * @rn;  -- @rn is now set to the total number of rows

关于mysql - 在选择Mysql中返回前N%个记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50938437/

10-10 18:38