我在MYSql中有一个名为companies的表,每个公司都有一个类型1类型,类型2类型3,
例如:
id company_name company_type
===============================
1 test1 3
2 xyz 2
3 ashdasdjk 2
4 test 4 1
5 test 3
6 ahsdkjsg 1
7 TCS 2
and so on ...
Run Code Online (Sandbox Code Playgroud)
现在我想写一个查询来获取结果,这样我就得到20个类型1的公司,20个公司的类型2和20个公司的类型3 ...我的意思是我想要获取最多20个公司的每种类型
我正在使用Codeigniter ..
select * from (
select
c.*,
@rn := if(company_type != @ct, 1, @rn + 1) as rownumber,
@ct := company_type
from
companies c
, (select @rn := 0, @ct := null) var_init
order by
company_type
) comp
where rownumber <= 20;
Run Code Online (Sandbox Code Playgroud)