【发布时间】:2018-06-20 00:00:39
【问题描述】:
我需要返回选择中所有记录的 33%
select id , amount, 'low'
from table 1
where amount > 1
order by 2;
所以我需要返回前 33% 的记录
【问题讨论】:
我需要返回选择中所有记录的 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
【讨论】: