我真的很困惑查询需要返回在特定列上具有最大值的前N行。
例如,如果行N-1, N, N + 1具有相同的值。我必须返回just top N还是要top N + 1排。
N-1, N, N + 1
just top N
top N + 1
如果您这样做:
select * from t order by value desc limit N
您将获得前N行。
select * from t join (select min(value) as cutoff from (select value from t order by value limit N ) tlim ) tlim on t.value >= tlim;
或者,您可以将其表达为:
select * from t join (select value from t order by value limit N ) tlim on t.value = tlim.value;
从概念上讲,以下是您想要执行的操作,但在MySQL中可能不起作用:
select * from t where t.value >= ANY (select value from t order by value limit N)