左外连接存储过程错误

时间:2015-09-15 04:59:21

标签: mysql stored-procedures left-join

我在这个存储过程中出错了。它成功执行但是当它在两个参数内调用时,我收到了这个错误

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   near' upper(t1.district_code)= t2.district_code)'在第1行

这是我的代码:

 DROP PROCEDURE IF EXISTS test_db.sp_temp_test;
 CREATE PROCEDURE test_db.`sp_temp_test`(in sdate DATE, edate DATE )
 begin
 DECLARE tbname VARCHAR(20);

 DECLARE y INT;
 DECLARE m INT;

 SET y = SUBSTRING(Year(sdate),3,2);  
 SET m = Month(sdate);
 SET tbname=''; 

 IF (10>m>0) THEN
 SET  tbname = CONCAT('post_0',m,y);
 ELSE
 SET  tbname = CONCAT('post_',m,y);
 END IF;



set @st = concat("select t1.*,t2.* ",
            " from (select a.event_source,b.Region,", 
            " a.cost as Value ,",
            "d.othre_names,d.sur_name, c.company_name,e.district_name",
            " from ", tbname," a, table_b b ,tabel_c c,test2db.District e",
             " where a.acc_no=d.acc_no",
            "  and a.event_date>='", sdate, "'  ",
            "  and a.event_date<='", edate, "'  ",
            "  and e.district_code=c.district_code",
            "  and a.event_type_id='11' ",      
            "  group by b.Region, a.event_type_id  ",
            "  order by  b.Region)t1  ",
            " left outer join testdb2.District t2",
            "on upper(t1.district_code)=t2.district_code");       

            prepare stmt from @st;
            execute stmt;
            deallocate prepare stmt;

            end;

3 个答案:

答案 0 :(得分:0)

尝试在&#34;之前添加空格&#34;在最后一行。您的串联不会在关键字之间添加单词分隔符。您最终会以&#34; ...左外连接 t2on 上方...&#34;

答案 1 :(得分:0)

您的SQL语句中有很多错误。这里有一些

  • v1 = {-1, -431602080, -431602080} v2 = {-1, -431602080, -431602080} v3 = {-.5, -431602080, -431602080} 仅使用group byb.Region,但您的选择中有更多列。这是不可能的。
  • 内部选择中的a.event_type_id无用,请移至最外部的选择。
  • 可能有一些输入错误,例如order by(而不是othre_names

您可以打印构建的sql语句并手动执行吗?你应该得到更好的错误信息。

答案 2 :(得分:0)

开启前添加一个空格。例如  &#34; on upper(t1.district_code)= t2.district_code&#34;);

相关问题