存储过程返回不正确的值

时间:2018-11-17 20:33:57

标签: mysql stored-procedures

我的查询返回正确的值,但是当我将查询作为存储过程运行时,它返回错误的结果。这是我的查询,返回正确的值

select imageId,imageVerified,isCoverImage,isDeleted,isProfileImage,path,profileId
 from images  where profileId = 5;

当我运行此存储过程时,它将返回所有行并显示profieId作为传入的profileId,这是我的存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `getImagesForUser`(in profileId long)
BEGIN
    select imageId,imageVerified,isCoverImage,isDeleted,isProfileImage,path,profileId
    from images  where profileId = profileId;
END

这就是我所谓的程序

CALL `tfm`.`getImagesForUser`(5);

请查看屏幕截图

用于查询的屏幕截图 screenshot for query

这是存储过程的错误结果

incorrect result from stored procedure

您可以看到,mysql说所有图像都属于我传入的profileId 5。 我的存储过程有什么问题

1 个答案:

答案 0 :(得分:1)

您的问题是您的输入参数与表中的字段具有相同的名称,并且在查询内部MySQL会将profileId解释为字段名称。因此,您的where profileId = profileId始终为true,并且得到所有行。更改输入参数的名称,例如

CREATE DEFINER=`root`@`localhost` PROCEDURE `getImagesForUser`(in searchProfileId long)
BEGIN
    select imageId,imageVerified,isCoverImage,isDeleted,isProfileImage,path,profileId
    from images  where profileId = searchProfileId;
END
相关问题