MySql 存储过程.... group_concat 中的错误

MySql Stored procedure.... error in group_concat

我有以下在 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

但是同样的查询在存储过程中给我错误

错误是:

You have an error in your SQL syntax; 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

存储过程是

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 ;

更新程序为

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 ;

错误是:在 MULTI DELETE

中未知 table 'c'

错误是什么,我该如何纠正?

此致

您正在设置一个值,因此不需要列别名... 删除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

并且您还错过了每次删除的命令终止符

   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命令。

其次,如果要使用 SELECT 查询的结果作为值,则必须在其两边加上括号:

SET @fence_ids = (SELECT ...);

三、当你使用:

WHERE af.fence_id IN (@fence_ids)

它将 @fence_ids 视为单个 ID,而不是 ID 列表。所以这相当于:

WHERE af.fence_id = @fence_ids

如果要在逗号分隔列表中搜索内容,需要使用 FIND_IN_SET:

WHERE FIND_IN_SET(af.fence_id, @fence_ids)

您也不应该在 GROUP_CONCAT().

中的值周围添加引号

但是你首先不应该为此使用 GROUP_CONCAT,你应该加入 returns 你想要的所有 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;

您不需要在两个语句中执行此操作:

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

你可以这样做:

SELECT ROW_COUNT() AS AROWS;