Create View语句中的MySQL用户定义变量

时间:2016-04-26 16:52:59

标签: mysql

在对stackoverflow进行一些搜索之后,我无法找到解决问题的方法。希望它并不那么难。

我试图将用户定义的变量用作创建视图语句的一部分。

我能够使用Prepare命令获得实际的select语句(如下所示)。我的问题是,当我尝试运行"创建视图"部分代码,我不断收到错误。不用多说,这里是MySQL代码:

这就是我想要做的事情(请注意,用户定义的变量位于"有"子句的最后)

create view `co 1_class` as 
select all_class_qry.job_number, all_class_qry.companyid, all_class_qry.classcode, all_class_qry.class, all_class_qry.`standard title`, sum(all_class_qry.`# incmbts`) as `sumof# incmbts`,
 avg(all_class_qry.`avgofannual base salary`) as `avgofavg base`, sum(all_class_qry.elig) as `sumof# recv`, avg(all_class_qry.`avgofbonus salary`) as `avgofavg bonus`, 
 avg(all_class_qry.`total cash`) as `avgoftotal cash`, avg(all_class_qry.`avgofposition min`) as `avgofavg min range`, avg(all_class_qry.`avgofposition mid`) as `avgofavg mid range`, 
 avg(all_class_qry.`avgofposition max`) as `avgofavg max range`
from all_class_qry
group by all_class_qry.job_number, all_class_qry.companyid, all_class_qry.classcode, all_class_qry.class, all_class_qry.`standard title`, all_class_qry.companyid
having (((all_class_qry.companyid)=@form_fwid_num));

就我所做的准备陈述而言:

set @co_1_class_qry = 'select all_class_qry.job_number, all_class_qry.companyid, all_class_qry.classcode, all_class_qry.class, all_class_qry.`standard title`, sum(all_class_qry.`# incmbts`) as `sumof# incmbts`,
 avg(all_class_qry.`avgofannual base salary`) as `avgofavg base`, sum(all_class_qry.elig) as `sumof# recv`, avg(all_class_qry.`avgofbonus salary`) as `avgofavg bonus`, 
 avg(all_class_qry.`total cash`) as `avgoftotal cash`, avg(all_class_qry.`avgofposition min`) as `avgofavg min range`, avg(all_class_qry.`avgofposition mid`) as `avgofavg mid range`, 
 avg(all_class_qry.`avgofposition max`) as `avgofavg max range`
from all_class_qry
group by all_class_qry.job_number, all_class_qry.companyid, all_class_qry.classcode, all_class_qry.class, all_class_qry.`standard title`, all_class_qry.companyid
having (((all_class_qry.companyid)=?));';

prepare stmt1 from @co_1_class_qry;
execute stmt1 using @form_fwid_num;

同样,我100%能够运行这三个命令^但我想提出

'create view `co 1_class` as'

就在@ co_1_class_qry字符串的开头。

我只能想象必须有一种方法可以将用户定义的变量用作create view / table语句的一部分,以及更好的方法。提前谢谢!

注意:这是MySQL不断收到的错误消息 -

09:27:09    prepare stmt1 from @co_1_class_qry  
Error Code: 1351. View's SELECT contains a variable or parameter    0.062 sec

0 个答案:

没有答案