错误代码:1064。您的SQL语法有错误;查看与MySQL服务器对应的手册

问题描述 投票:0回答:1

以下存储过程给我一个SQL错误。我无法弄清楚是什么导致了它。

    DROP TABLE IF EXISTS tmp_results_dailyearnings;

CREATE TEMPORARY TABLE tmp_results_dailyearnings AS
        SELECT emp_id, date_, sum(ifnull(reg_hours,0)) reg_hours, sum(ifnull(ot_hours,0)) ot_hours, SUM(ifnull(allowance,0)) allowance,
                (sum(ifnull(reg_hours,0)) + sum(ifnull(ot_hours,0)) + SUM(ifnull(allowance,0))) gross, SUM(ifnull(LEAV,0)) SIL
        FROM vw_totaldailyearnings
        WHERE date_ between "2018-05-08" and "2018-05-21"
        GROUP BY emp_id, date_
        ORDER BY date_ ASC;

            SELECT CONCAT('
            SELECT emp_id, concat(b.last_name,'','',b.first_name,'','',b.middle_name) name, ',cores_by_dates,', IFNULL(ot_hours,0) ot_hours, IFNULL(allowance,0) allowance,
                    gross, SIL
            FROM tmp_results_dailyearnings a
            inner join tbl_employee b
            on a.emp_id = b.id
            GROUP BY a.emp_id, a.ot_hours, a.allowance, a.gross
            ORDER BY a.emp_id'
            )
            INTO @query
            FROM
            (
            SELECT GROUP_CONCAT(CONCAT('IFNULL(MAX(CASE WHEN date_=''',actual_date,''' THEN reg_hours END), ''0'') AS "',col_name,'"')) cores_by_dates
            FROM (
            SELECT actual_date, DATE_FORMAT(actual_date,'%a %m/%d') AS col_name
            FROM (SELECT DISTINCT date_ AS actual_date FROM tmp_results_dailyearnings) AS dates
            ) dates_with_col_names
            ) result;

            drop table if exists tmp_earningsReport;
            SET @createSQL = CONCAT('CREATE TEMPORARY TABLE tmp_earningsReport AS ', @query);
            PREPARE statement FROM @createSQL;
            EXECUTE statement;
            DEALLOCATE PREPARE statement;

我不断收到的错误消息是检查与您的MySQL服务器版本对应的手册,以便在'ifnull(ot_hours,0)ot_hours,IFNULL(allowance,0)allowance,gross,0 SIL'附近使用正确的语法2

请帮忙。谢谢!

mysql
1个回答
0
投票

错误的报价序列..在您的代码中为字符串选择您只使用单引号但如果您需要包装sql代码您应该使用双引号以避免冲突输入单引号

 SELECT CONCAT("
        SELECT emp_id, concat(b.last_name,'','',b.first_name,'','',b.middle_name) name, ',cores_by_dates,', IFNULL(ot_hours,0) ot_hours, IFNULL(allowance,0) allowance,
                gross, SIL
        FROM tmp_results_dailyearnings a
        inner join tbl_employee b
        on a.emp_id = b.id
        GROUP BY a.emp_id, a.ot_hours, a.allowance, a.gross
        ORDER BY a.emp_id'" 
        )
© www.soinside.com 2019 - 2024. All rights reserved.