You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL sakila库自定义存储过程查询影片信息始终返回1000行问题求助

问题根因

你遇到的问题核心是存储过程输入参数名和表字段名完全重名:
MySQL默认对标识符大小写不敏感,解析WHERE条件时会优先将Language_Id、Category_Id识别为表的列名,而非存储过程的输入参数。因此你写的where language_id = Language_Id等价于where 1=1恒成立,所以查询始终返回film表全量1000行数据。

修复方案

给存储过程输入参数增加统一前缀(比如p_),避免和表字段名冲突即可,修改后的代码如下:

drop procedure if exists `displayFilmInfo`;

delimiter $$
create procedure displayFilmInfo (in p_Category_Id tinyint unsigned,
                                in p_Language_Id tinyint unsigned)
begin
if p_Category_Id = 0 then
select * from film 
where language_id = p_Language_Id;
else if p_Language_Id = 0 then
    select film.* from
    film join film_category
    on film.film_id = film_category.film_id
    where category_id = p_Category_Id;
    else
        select film.* from
        film join film_category
        on film.film_id = film_category.film_id
        where category_id = p_Category_Id
        and film.language_id = p_Language_Id;
    end if;
end if;
end $$
delimiter ;
补充说明

如果不想修改参数名,也可以通过存储过程名限定参数的方式调用,例如where language_id = displayFilmInfo.Language_Id,但该写法可读性差,更推荐修改参数名的方案。

验证测试

可执行以下调用验证效果:

  • 查语言ID为1的所有影片:call displayFilmInfo(0,1);
  • 查分类ID为1的所有影片:call displayFilmInfo(1,0);
  • 查同时属于分类1、语言1的影片:call displayFilmInfo(1,1);
    三种场景返回的行数会符合业务预期,不会再固定返回1000行。

内容的提问来源于stack exchange,提问作者user14470590

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 07:15:01