左外连接存储过程错误

Left outer join Stored Procedure Error

我在这个存储的 procedure.It 中得到一个错误执行成功但是当它在两个参数内调用时,我得到这个错误

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 'upper(t1.district_code)=t2.district_code)' at line 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;

尝试在最后一行的 "on" 之前添加一个 space。您的连接不会在关键字之间添加单词分隔符。您将以“...左外连接 t2on 上...”结束

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

  • group by 仅使用 b.Regiona.event_type_id,但您的 select 中有更多列。这是不可能的。
  • 内select里的order by没用,移到最外select.
  • 可能有一些打字错误,例如 othre_names(而不是 other_names

你能打印你构造的sql语句并手动执行吗?您应该会收到更好的错误消息。

在on之前加一个space。例如 “上层(t1.district_code)=t2.district_code”);