2

对这个问题的任何帮助都会很好,因为它让我发疯。我的 MYSQL 数据库中的一个表包含货币硬币的详细信息,尽管那里似乎有很多动态 WHERE 子句,但我搜索了几十个似乎都没有满足缺失的变量。

TABLE coin (
  coinID smallint(6) NOT NULL AUTO_INCREMENT,
  coinCountry smallint(6) NOT NULL,
  coinDenom smallint(6) NOT NULL,
  coinMinted smallint(6) NOT NULL,
  coinImageLocation varchar(128) NOT NULL,
  coinPgeTitle varchar(45) NOT NULL,
  PRIMARY KEY (coinID)
) 

我想要一个存储例程中的动态“WHERE”子句,它接受三个参数,其中一个参数是强制性的,并且总是有一个值,另外两个是任意的,要么有一个值,要么没有

我已经编写了这个存储的例程,并且在某个地方隐藏了问题

DELIMITER $$


CREATE DEFINER=`root`@`localhost` PROCEDURE `procFilterCoins`
(

/*******************************************************

Proc     :  Filter the coins returned by adjusting the WHERE Clause.

IN   :  iCountry (The CountryID) a mandatory value

IN   :  iDenom (The DenominationID) a discretionary value

IN   :  iMinted (The year minted) a discretionary value

OUT      :  filtered coins recordset

********************************************************/

IN iCountry int(11), 

IN iDenom int(11), 

IN iMinted int(11)
)

BEGIN

/* somewhere to hold our where clause*/

DECLARE strWhere varchar(100);


set strWhere = '((coinCountry = ';

IF iDenom > 0 and iDenom IS NOT NULL THEN

    set strWhere = concat(strWhere, iCountry, ") AND (coinDenom = ", iDenom, ')');

ELSE

    set strWhere = concat(strWhere, iCountry, ')');

END IF;


IF iMinted > 0 and iMinted IS NOT NULL THEN

    set strWhere = concat(strWhere, " AND (coinMinted = ", iMinted, '))');

ELSE

    set strWhere = concat(strWhere, ')');
END IF;


/*Extract the records via prepared view*/
SELECT * FROM vAllcoins WHERE + strwhere;


END

我使用MySQL Workbench 5.2.46.CE测试了以下数据

调用 procfiltercoins(18,null,1948); 生产... WHERE ((coinCountry = 18) AND (coinMinted = 1948)); 没有返回记录

调用 procfiltercoins(103,4,1948,@out); 生产...

WHERE ((coinCountry = 103) AND (coinDenom = 4) AND (coinMinted = 1948));

没有返回记录

调用 procfiltercoins(18,2,null,@out); 生产...

WHERE ((coinCountry = 18) AND (coinDenom = 2));

没有返回记录

我已经在 Access 数据库中复制了所有这些,没有任何问题

4

2 回答 2

5

尝试使用准备好的语句

SET @query = CONCAT("SELECT * FROM vAllcoins WHERE ",strwhere);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

不确定要对查询结果做什么。把它放在一个视图中可能吗?

SET @query = CONCAT("CREATE VIEW myView as SELECT * FROM vAllcoins WHERE ",strwhere);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

祝你好运!

于 2013-06-20T10:04:38.423 回答
-1

我已经重新调整了存储的例程并提出了以下可行的方法。

CREATE DEFINER=root@localhost PROCEDURE procOtherFilter( /****************************** Proc : Filter the coins returned by adjusting the WHERE Clause. IN : iCountry (The CountryID) a mandatory value IN : iDenom (The DenominationID) a discretionary value IN : iMinted (The year minted) a discretionary value OUT : filtered coins recordset *******************************/ IN iCountry int(11), IN iDenom int(11), IN iMinted int(11) ) BEGIN /* If we only have the countryID it will always be there as it's mandatory */ IF (iDenom IS NULL) AND (iMinted IS NULL) THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry); /* If the denomination is missing use the minted date */ ELSEIF iDenom IS NULL THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry) AND (coinMinted = iMinted); /* If the minted date is missing use the denomination */ ELSEIF iMinted IS NULL THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry) AND (coinDenom = iDenom);

END IF;

END

感谢所有观看的人

于 2013-06-22T08:30:10.490 回答