我在MYSql中有一个称为公司的表格,每个公司的类型都说类型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 ...
现在我想写一个查询来获取结果,以便我得到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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句