在where子句不起作用的情况下附加条件

时间:2011-10-10 07:00:05

标签: mysql sql concat

我正在尝试将条件附加到where子句。但是包含条件的字符串不起作用。我没有收到任何错误,但条件未在查询中完全应用

这是查询。我将CLINIC_ID和taleEnd传递给我的存储过程.taleEnd将包含一个或更多这样的条件 - “和'2011-09-08'和'2011-10-08'之间的DOS”

SELECT *
  FROM A
  JOIN B ON C.CLINIC_ID = VP.CLINIC_ID AND C.CLAIM_ID = VP.CLAIM_ID
  JOIN C ON P.CLINIC_ID = VP.CLINIC_ID  AND P.PATIENT_ID = VP.PATIENT_ID
  JOIN D ON I.CLINIC_ID = C.CLINIC_ID  AND I.INSURANCE_ID= C.PRIMARY_INSURANCE_ID
  JOIN E ON B.CLINIC_ID = I.CLINIC_ID  AND B.BUSINESS_ID= I.COMPANY_ID
 WHERE (VP.STATUS = 3 OR VP.STATUS = 5)
   AND VP.PRIMARY_PAID = 0
   AND VP.PRIMARY_PENDING > 0
   AND C.PRIMARY_PAYER_ID > 0
   AND C.HIDEN = 0
   AND VP.CLINIC_ID = CLINIC_ID + taleEnd

问题是当我运行存储过程时只应用了clinic_id而不是DOS。我尝试了concat,但是没有工作。我没有得到任何错误但是结果不适用于DOS。可以任何人帮助我请。

@Devart这是商店程序。

CREATE DEFINER=`root`@`%` PROCEDURE `PRIMARY_INSURANCE_AGING`(CLINIC_ID INT,taleEnd TEXT)
BEGIN
DROP TEMPORARY TABLE IF EXISTS PRIMARY_TEMP;
CREATE TEMPORARY TABLE PRIMARY_TEMP
SELECT *     
  FROM A   
  JOIN B ON C.CLINIC_ID = VP.CLINIC_ID AND C.CLAIM_ID = VP.CLAIM_ID   
  JOIN C ON P.CLINIC_ID = VP.CLINIC_ID AND P.PATIENT_ID = VP.PATIENT_ID   
  JOIN D ON I.CLINIC_ID = C.CLINIC_ID  AND I.INSURANCE_ID= C.PRIMARY_INSURANCE_ID   
  JOIN E ON B.CLINIC_ID = I.CLINIC_ID  AND B.BUSINESS_ID= I.COMPANY_ID 
 WHERE (VP.STATUS = 3 OR VP.STATUS =5 ) 
   AND VP.PRIMARY_PAID = 0   
   AND VP.PRIMARY_PENDING > 0   
   AND C.PRIMARY_PAYER_ID > 0   
   AND C.HIDEN = 0  
   AND VP.CLINIC_ID = CLINIC_ID + taleEnd;

   END

这是Prepare声明

的用法
SET @A = CONCAT(34847," and DOS between '2011-09-08' and '2011-10-08'");
PREPARE STMT FROM '
SELECT *
  FROM A
  JOIN B ON C.CLINIC_ID = VP.CLINIC_ID AND C.CLAIM_ID = VP.CLAIM_ID
  JOIN C ON P.CLINIC_ID = VP.CLINIC_ID AND P.PATIENT_ID = VP.PATIENT_ID
  JOIN D ON I.CLINIC_ID = C.CLINIC_ID AND I.INSURANCE_ID= C.PRIMARY_INSURANCE_ID
  JOIN E ON B.CLINIC_ID = I.CLINIC_ID AND B.BUSINESS_ID= I.COMPANY_ID
  WHERE (VP.STATUS = 3 OR VP.STATUS = 5)
  AND VP.PRIMARY_PAID = 0
  AND VP.PRIMARY_PENDING > 0
  AND C.PRIMARY_PAYER_ID > 0
  AND C.HIDEN = 0
  AND VP.CLINIC_ID ? ';

EXECUTE STMT USING @A;

2 个答案:

答案 0 :(得分:0)

如果你将CLINIC_ID传递给存储过程,并尝试在过程中使用它,我认为(无法正确检查),变量{{1}之间存在混淆}和字段CLINIC_ID

尝试将过程中的变量名称更改为唯一的名称。

答案 1 :(得分:0)

与Dave Rix就变量和字段名称达成一致,它们应该是不同的。

如果要在查询中附加额外的字符串(如“2011-09-08”和“2011-10-08”之间的 - 和DOS),则应使用{{ 3}}

修改

示例:

SET @query = 'SELECT *
FROM A
JOIN B ON C.CLINIC_ID = VP.CLINIC_ID AND C.CLAIM_ID = VP.CLAIM_ID
JOIN C ON P.CLINIC_ID = VP.CLINIC_ID AND P.PATIENT_ID = VP.PATIENT_ID
JOIN D ON I.CLINIC_ID = C.CLINIC_ID AND I.INSURANCE_ID= C.PRIMARY_INSURANCE_ID
JOIN E ON B.CLINIC_ID = I.CLINIC_ID AND B.BUSINESS_ID= I.COMPANY_ID WHERE (VP.STATUS = 3 OR VP.STATUS =5 ) AND VP.PRIMARY_PAID = 0
AND VP.PRIMARY_PENDING > 0
AND C.PRIMARY_PAYER_ID > 0
AND C.HIDEN = 0
AND VP.CLINIC_ID = ?'; -- ? will be supplied with procedure argument clinic_id_value; where clinic_id_value is a renamed procedure argument

IF taleEnd IS NOT NULL THEN
  @query = CONCAT(@query, ' ', taleEnd);
END IF;

SET @clinic_id_value = clinic_id_value; -- copy procedure argument to the local variable
PREPARE STMT FROM @query;
EXECUTE STMT USING @clinic_id_value;
DEALLOCATE PREPARE STMT;