错误发生在另一个设置上,而不是我的:"Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in MySQL"

The error occurs on another setup, not on mine: "Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in MySQL"

我的问题是link下面的问题:

就我而言,下面的触发器在我的本地设置上运行良好(LAMPP on Ubuntu):

-- trigger to increment balance on posting of a new bill
DELIMITER
    //
CREATE TRIGGER trig_increment_balance_new_bill AFTER INSERT ON
    tbl_bill FOR EACH ROW
BEGIN
    DECLARE
        vBillAmount DOUBLE ;
    SELECT NEW
        .amount
    INTO vBillAmount ;
UPDATE
    tbl_client T
SET
    balance = balance + vBillAmount
WHERE
    id =(
    SELECT
        id
    FROM
        tbl_client C
    WHERE NEW
        .metreNo = C.metreNo
    LIMIT 1
) ;
END ; //
DELIMITER
    ;

但是,我的协作者的设置(XAMPP 在 Windows 上)时出现以下错误重新创建相同的代码并进行测试。

SQLSTATE[HY000]: General error: 1093 Table 'T' is specified twice, both as a target for 'UPDATE' and as a separate source for data

我以为是严格模式有关,结果发现是严格模式开启; 'STRICT_ALL_TABLES' 就我而言。为什么他的设置会出错,而我的却不会?我知道上面 link 中提供的解决方案可能会解决此错误,但问题是为什么错误会选择性地发生?

不需要子查询,直接把条件放在WHERE子句中即可。您也不需要 vBillAmount 变量。

CREATE TRIGGER trig_increment_balance_new_bill AFTER INSERT ON tbl_bill 
FOR EACH ROW
    UPDATE tbl_client c 
    SET c.balance = c.balance + NEW.amount 
    WHERE c.metreNo = NEW.metreNo
    LIMT 1;