简体   繁体   English

如何在MySQL中将动态列名传递给函数参数?

[英]How to pass a Dynamic column name to a function parameter in MySql?

Okay, I'm only looking for a work around now. 好的,我现在只是在寻找工作。 Could some one guide me? 有人可以指导我吗?

EDIT: 编辑:
Okay, So apparantly there's no way to get a function to call a dynamic procedure in MySql. 好的,因此显然没有办法在MySql中获得函数来调用动态过程。
But I really need to do this select statement dynamically! 但是我真的需要动态地执行此select语句!

SELECT Location FROM DemoTable WHERE No_of_Hospitals > AvgCity(No_of_Hospitals)

Or find a better way to deal with this problem. 或者找到一种更好的方法来处理此问题。 The parameters in the AvgCity() will be dynamic all the time. AvgCity()中的参数将始终保持动态。 That's kind of compulsory. 这是强制性的。 The parameter passing will be the name of a column from the DemoTable. 传递的参数将是DemoTable中列的名称。 I need an efficient way to handle the Rules from the Rule Table and the DemoTable as I've stated below. 我需要一种有效的方法来处理规则表和DemoTable中的规则,如下所述。 Its been 3 days now that i've been stuck on this. 现在已经三天了,我一直坚持下去。 HELP!! 救命!! :s :s

For reference, here's the old problem i had. 供参考,这是我遇到的老问题。 And the flow of everything that i've got done so far- 到目前为止,我所完成的所有工作都顺其自然,

Old Problem: 旧问题:
Could some one help me out with a good way to go about this? 有人可以帮我解决这个问题的好方法吗? I'm pretty new to MySql and I can't seem to find a simple answer for this anywhere. 我是MySql的新手,我似乎在任何地方都找不到简单的答案。
I have two tables that I'm working with. 我有两个正在使用的表。 The first table contains a lot of columns filled with Integer values such as: 第一个表包含许多用Integer值填充的列,例如:

 No_of_Hotels  No_of_Hospitals  

The second table is a Rule Table which contains a rule name followed by the actual rule. 第二个表是“规则表”,其中包含规则名称和实际规则。
Eg: 例如:

ID RuleName Rule 1 Example No_of_Hospitals > AvgCity(No_of_Hospitals)

The function that I have right now is: 我现在拥有的功能是:

 CREATE FUNCTION AvgCity(columnName text) RETURNS float(10)  
 DETERMINISTIC  
 BEGIN  
 DECLARE columnAvg float;  
 SELECT AVG(columnName)   
 INTO columnAvg 
 FROM DemoTable;
 RETURN (columnAvg);  
 END

But every time I pass a value to the function parameter - which is the name of one of the columns that I want the average for, I get a 0 in return. 但是,每当我将一个值传递给函数参数-这是我想要平均值的列之一的名称时,我都会得到0
I figured that this is happening because the parameter being passed into the AVG() Function is being passed as a string and not a column name. 我发现发生这种情况是因为传递给AVG()函数的参数是作为字符串而不是列名传递的。 So that's AVG("No_of_Hospitals") instead of AVG(No_of_Hospitals) which also returns 0 . 因此,这是AVG("No_of_Hospitals")而不是AVG(No_of_Hospitals) ,它也返回0
How do I fix this and get a result? 我该如何解决并得到结果?

I'll be executing the whole thing after the result with: 我将在执行结果后执行整个操作:

SELECT @Q:= CONCAT('SELECT Location FROM DemoTable WHERE ', Rules.Rule) FROM
Rules     WHERE ID=1;
PREPARE stq FROM @Q;
EXECUTE stq;
DEALLOCATE PREPARE stq;

It all goes in a SELECT statement so, I do need the AvgCity() to be a Function and NOT a Procedure. 所有这些都在SELECT语句中进行,因此,我确实需要AvgCity()作为函数而不是过程。

UPDATE 1: 更新1:
So I decided to put a procedure inside the function. 因此,我决定在该函数中放置一个过程。
What i have now is this function: 我现在所拥有的就是这个功能:

CREATE FUNCTION AvgCity(colName text) RETURNS float(10)
DETERMINISTIC
BEGIN
DECLARE colAvg float;
CALL AvgCityProcedure(colName,colAvg);
RETURN (colAvg);
END    

And this Procedure: 并且此过程:

CREATE PROCEDURE AvgCityProcedure(
IN colName VARCHAR(100),
OUT colAvg FLOAT)
BEGIN
SET @c1 = 0.0;
SET @M:= colName;
SET @QUERY:= CONCAT('SELECT AVG(',@M,') INTO @C1 FROM DemoTable');
PREPARE stmt FROM @QUERY;
EXECUTE stmt; 
SET colAvg:=@C1;
SELECT colAvg;
END   

UPDATE 2: I got the procedure working! 更新2: 我使程序正常工作!
Followed this really nice example: 遵循了这个非常好的示例:
My SQL Dynamic query execute and get ouput into a variable in stored procedure 我的SQL动态查询执行并输出到存储过程中的变量中
But i can't seem to manage to insert any value inside colAvg . 但是我似乎无法设法在colAvg内插入任何值。 I tried a lot of combinations with the @QUERY , but i can't find the right one. 我使用@QUERY尝试了很多组合,但找不到合适的组合。 I keep ending up with ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 我一直以ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1结尾ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

But if i remove the INTO colAvg part and just do SELECT @QUERY:= CONCAT('SELECT AVG(',@M,') FROM DemoTable'); 但是如果我删除INTO colAvg部分,然后执行SELECT @QUERY:= CONCAT('SELECT AVG(',@M,') FROM DemoTable');
It totally prints the answer to screen. 它完全将答案打印到屏幕上。 I need to return that to colAvg . 我需要将其返回给colAvg What am i doing wrong? 我究竟做错了什么?
Alright! 好的! I got the Procedure working properly! 我让程序正常工作! I got another problem now. 我现在遇到另一个问题。 I get an error while returning values from my function to the procedure. 从函数返回值到过程时出现错误。
It says dynamic sql not allowed inside the function. 它说函数内部不允许使用动态SQL。
But i used the dynamic sql inside my procedure :S 但是我在过程中使用了动态SQL:S
Whats happening here?! 这里发生了什么事?!
What I need it to look like in the end is: 我需要的最终外观是:
SELECT Location FROM DemoTable WHERE No_of_Hospitals > AvgCity(No_of_Hospitals)

Could someone please set up a small bounty on this? 有人可以为此设置一个小小的赏金吗? I haven't got a single answer in more than a week now. 现在有一个多星期了,我没有一个答案。 :( :(

我只是将整个查询放在一个字符串中(使用concat,包括表名)并执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM