这是 MySQL 中的存储过程:
CREATE PROCEDURE topModels(modelCount INT)
BEGIN
select brand_name as brandName,
model_number as modelNumber,
model_description as modelDescription,
parts
from
(
select model_number, model_description, parts
,@rownum := if(@brand=t.brand_name, @rownum, 0) + 1 as rownum
,@brand := t.brand_name as brand_name
from
(
select brand_name, model_number, model_description, parts,
from model f
group by brand_name, model_number, model_description
order by brand_name, sum(sold_count) desc
) t
) tbl
where rownum<=5
END
当我打开 MySQL 工作台并执行以下操作时:
CALL topModels(5)
它返回大约 3000 行,这是正确的结果。
当我从 Java 运行它时:
java.sql.CallableStatement cs = connection.
prepareCall("CALL topModels(5)");
java.sql.ResultSet rs = cs.executeQuery();
它返回大约 400,000 行,就好像它忽略了“where rownum<=5”条件一样。
我做错了什么?