首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >返回select Mysql中的前N%条记录

返回select Mysql中的前N%条记录
EN

Stack Overflow用户
提问于 2018-06-20 08:00:39
回答 1查看 92关注 0票数 1

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

代码语言:javascript
复制
select id , amount, 'low'
from table 1 
where amount > 1
order by 2;

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 08:02:28

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50938437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档