有时需求,对海量数据中按条件查询,但只要前若干行数据的话,适合本SQL算法;
SET @rn = 0;
SET @[要限定的字段变量]= '';
SELECT
*, @rn:= if(@[要限定的字段变量]=`字段名`, @rn + 1, 1) AS `计数`,
@[字段名]:= `[字段名]` AS `字段内容项`,
@rn AS `inner_row_num`
FROM
`[表名]`
GROUP BY
`[字段名]`,
`inner_row_num`
HAVING `inner_row_num` <= [限定的行数];