简体   繁体   English

MySql存储过程…group_concat中的错误

[英]MySql Stored procedure… error in group_concat

I have following query that works in phpMyAdmin 我有以下查询可在phpMyAdmin中使用

SELECT CONCAT("'",GROUP_CONCAT( fence_id SEPARATOR "','" ),"'") AS fence_ids 
FROM asset_fence af 
INNER JOIN assets a ON a.vehicle_id = af.vehicle_id 
WHERE a.client_id=1

But the same query gives me error in stored procedure 但是相同的查询给我存储过程中的错误

ERROR IS: 错误是:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@fence_ids = SELECT CONCAT("'",GROUP_CONCAT( fence_id SEPARATOR "','" ),"'") AS ' at line 8 检查与您的MariaDB服务器版本相对应的手册,以在第8行的'@fence_ids = SELECT CONCAT(“'',GROUP_CONCAT(fence_id SEPARATOR”','“),”'“)AS'附近使用正确的语法

STORED PROCEDURE IS 存储过程为

DELIMITER $$

CREATE PROCEDURE `prcDeleteClient`(IN `f_client_id` INT, OUT AROWS INT)
BEGIN


    START TRANSACTION;


        @fence_ids = SELECT CONCAT("'",GROUP_CONCAT( fence_id SEPARATOR "','" ),"'") AS fence_ids FROM asset_fence af INNER JOIN assets a ON a.vehicle_id = af.vehicle_id WHERE a.client_id=f_client_id


        DELETE
            asset_fence,
            geo_fence

        FROM 
            geo_fence gf INNER JOIN asset_fence af ON gf.fence_id = af.fence_id

        WHERE
            af.fence_id IN (@fence_ids)



        DELETE
            client,
            assets,
            asset_movement

        FROM 
            asset_movement am INNER JOIN assets a ON am.vehicle_id = a.vehicle_id
            assets a INNER JOIN client c ON a.client_id = c.client_id

        WHERE
            c.client_id=f_client_id


        SET @AROWS = ROW_COUNT();
        SELECT @AROWS as AROWS;

    COMMIT;
END $$

DELIMITER ;

Updated procedure is 更新的程序是

DELIMITER $$

CREATE PROCEDURE `prcDeleteClient`(IN `f_client_id` INT, OUT AROWS INT)
BEGIN


    START TRANSACTION;


        DELETE
            af,
            gf
        FROM 
            geo_fence gf

            INNER JOIN asset_fence af ON gf.fence_id = af.fence_id
            INNER JOIN assets a ON a.vehicle_id = af.vehicle_id

        WHERE
            a.client_id=f_client_id;



        DELETE
            c,
            a,
            am

        FROM 
            asset_movement am INNER JOIN assets a ON am.vehicle_id = a.vehicle_id
            assets a INNER JOIN clients c ON a.client_id = c.client_id

        WHERE
            c.client_id=f_client_id;


        SELECT ROW_COUNT() AS AROWS;

    COMMIT;
END $$

DELIMITER ;

Error is: Unknown table 'c' in MULTI DELETE 错误是:MULTI DELETE中的未知表'c'

What is the mistake and how can I rectify it? 有什么错误,我该如何纠正?

Best Regards 最好的祝福

You are setting a value so you don't need the column alias ... remove the AS fence_ids 您正在设置一个值,因此不需要列别名...删除AS fence_ids

    SELECT CONCAT("'", GROUP_CONCAT( af.fence_id SEPARATOR "','" ),"'") 
    FROM asset_fence af 
    INNER JOIN assets a ON a.vehicle_id = af.vehicle_id
    WHERE a.client_id=1

and you missed also command terminater for each delete 并且您还错过了每次删除的命令终止器

   DELETE
        asset_fence,
        geo_fence

    FROM 
        geo_fence gf INNER JOIN asset_fence af ON gf.fence_id = af.fence_id

    WHERE
        af.fence_id IN (@fence_ids);



    DELETE
        client,
        assets,
        asset_movement

    FROM 
        asset_movement am INNER JOIN assets a ON am.vehicle_id = a.vehicle_id
        assets a INNER JOIN client c ON a.client_id = c.client_id

    WHERE
        c.client_id=f_client_id;

First, your syntax for assigning variables is wrong, it needs the SET command before the variable name. 首先,您分配变量的语法是错误的,它需要在变量名称之前使用SET命令。

Second, if you want to use the result of a SELECT query as a value, you have to put parentheses around it: 其次,如果要使用SELECT查询的结果作为值,则必须在其周围加上括号:

SET @fence_ids = (SELECT ...);

Third, when you use: 第三,使用时:

WHERE af.fence_id IN (@fence_ids)

it will treat @fence_ids as a single ID, not a list of IDs. 它将@fence_ids视为单个ID,而不是ID列表。 So this is equivalent to: 因此,这等效于:

WHERE af.fence_id = @fence_ids

If you want to search for something in a comma-separated list, you need to use FIND_IN_SET : 如果要搜索逗号分隔列表中的内容,则需要使用FIND_IN_SET

WHERE FIND_IN_SET(af.fence_id, @fence_ids)

You also shouldn't add quotes around the values in your GROUP_CONCAT() . 您也不应在GROUP_CONCAT()的值周围加上引号。

But you shouldn't use GROUP_CONCAT for this in the first place, you should just join with the query that returns all the IDs you want. 但是,您首先不应为此使用GROUP_CONCAT ,而应与返回所需的所有ID的查询一起使用。

   DELETE
        af,
        gf
    FROM 
        geo_fence gf 
    INNER JOIN asset_fence af ON gf.fence_id = af.fence_id
    INNER JOIN assets a ON a.vehicle_id = af.vehicle_id 
    WHERE a.client_id=f_client_id;

You don't need to do this in two statements: 您无需在两个语句中执行此操作:

SET @AROWS = ROW_COUNT();
SELECT @AROWS as AROWS;

You can just do: 您可以这样做:

SELECT ROW_COUNT() AS AROWS;

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

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