I have a requirement where i need to select distinct employers id's with active jobs, however hard part is i have to restrict the query to always return the even number of rows.
我有一个要求,我需要选择不同的有活动工作的雇主id,无论我必须限制查询总是返回偶数行。
For example
例如
if the distinct employers with active jobs are 10, then it can return 10, which is even then no problems, but if the number of employers with the active jobs are 9 then it should limit it to 8.
如果不同的雇主有不同的积极工作是10,那么它可以返回10,这是没有问题的,但是如果有积极工作的雇主是9,那么它应该限制在8。
Notes: this requirement is to make sure page has even number of rows will make the page balance in the display. Secondly i know i can do this on the coding, but i am looking more of a MySQL query based solution if possible as the results are cached in the memcached, it makes things complicated in the php to do this in coding level.
注意:这个要求是为了确保页面有偶数行,使页面在显示中保持平衡。第二,我知道我可以在编码中这样做,但是如果可能的话,我更倾向于使用基于MySQL查询的解决方案,因为结果被缓存在memcached内,这使得在php中在编码级别执行这一操作变得非常复杂。
1 个解决方案
#1
3
It's not clear what do you want to do with the last record if count is odd. Also I think you should do this on the client side. For example you have table A
with the field val
. You need even number of distinct val
:
如果count是奇数,那么你想怎么处理最后一个记录呢?而且我认为你应该在客户端这样做。例如,你有表格A和字段val。你需要偶数个不同的val:
SQLFiddle演示
select val
from
(
select val,
@rn:=@rn+1 rownum
from (select distinct val from A) A1,
(select @rn:=0) t1
order by val
) T2
where rownum<=
((select count(distinct val) from A) div 2)*2
#1
3
It's not clear what do you want to do with the last record if count is odd. Also I think you should do this on the client side. For example you have table A
with the field val
. You need even number of distinct val
:
如果count是奇数,那么你想怎么处理最后一个记录呢?而且我认为你应该在客户端这样做。例如,你有表格A和字段val。你需要偶数个不同的val:
SQLFiddle演示
select val
from
(
select val,
@rn:=@rn+1 rownum
from (select distinct val from A) A1,
(select @rn:=0) t1
order by val
) T2
where rownum<=
((select count(distinct val) from A) div 2)*2