mysql函数参数在select语句的条件下不起作用

时间:2018-09-04 09:14:05

标签: mysql mysqli

DELIMITER $$
     CREATE DEFINER=`axistms`@`localhost` FUNCTION `CheckDoc`(`orderId` INT) RETURNS int(11)
DETERMINISTIC
BEGIN
     DECLARE lvl int;
     SELECT count(`id`) INTO lvl FROM `com_carrier_portal_upload_documents` WHERE `orderID`= orderId;
     RETURN lvl;
END$$
DELIMITER ;

任何订单ID都通过orderId参数传递,它对where条件不起作用。始终返回所有记录的计数。如何解决此问题?

1 个答案:

答案 0 :(得分:1)

我建议您重命名存储过程参数,执行以下操作-

CREATE DEFINER = `axistms`@`localhost` FUNCTION `CheckDoc` (orderIdParam int)
RETURNS int(11)
DETERMINISTIC
BEGIN
  DECLARE lvl int;
  SELECT
    COUNT(`id`) INTO lvl
  FROM `com_carrier_portal_upload_documents`
  WHERE `orderID` = orderIdParam;
  RETURN lvl;
END

...因为WHERE orderID = orderId可以等于WHERE true。