【问题标题】:Return first N% of records in select Mysql [duplicate]返回选择Mysql中的前N%的记录[重复]
【发布时间】:2018-06-20 00:00:39
【问题描述】:

我需要返回选择中所有记录的 33%

select id , amount, 'low'
from table 1 
where amount > 1
order by 2;

所以我需要返回前 33% 的记录

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    我认为您需要枚举行,除了最新版本的 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
    

    【讨论】:

    • thanx ,它正是。什么 。我试图实现
    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2015-05-18
    • 2014-11-19
    • 2019-01-23
    • 2019-04-25
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多