1

我有这个工作查询:

SELECT idVendRigMov
FROM VendRM
WHERE idArtPromo IS NULL AND
      (idArtCategoria NOT IN (SELECT idArtCategoria FROM VendScontiCatEscl WHERE idVendSconti=2) OR idArtCategoria IS NULL) AND idVendTesMov=1

返回“idVendRigMov”列表;

我需要对此结果的每一行进行更新,如下所示:

UPDATE VendRigMov SET Sconto='7' WHERE idVendRigMov IN (result of previous query);

我试过这个:

UPDATE VendRigMov SET Sconto='7' WHERE idVendRigMov IN 
(
SELECT idVendRigMov FROM VendRM WHERE idArtPromo IS NULL AND (idArtCategoria NOT IN (SELECT idArtCategoria FROM VendScontiCatEscl WHERE idVendSconti=2) OR 
idArtCategoria IS NULL) AND idVendTesMov=1
)

但我收到此错误:“表“VendRM”的定义阻止对表“VendRigMov”进行更新操作”

我该怎么做这个查询?

4

1 回答 1

1

VendRM可能是指表的视图VendRigMov。如果是这样,您可以通过使用附加级别的子查询来解决此问题。您的查询是:

UPDATE VendRigMov
    SET Sconto = '7'
    WHERE idVendRigMov IN (SELECT idVendRigMov
                           FROM VendRM
                           WHERE idArtPromo IS NULL AND
                                 (idArtCategoria NOT IN (SELECT idArtCategoria
                                                         FROM VendScontiCatEscl
                                                         WHERE idVendSconti=2) OR 
                                                               idArtCategoria IS NULL
                                                        ) AN
                                idVendTesMov=1
                          )

这应该有效:

UPDATE VendRigMov
    SET Sconto = '7'
    WHERE idVendRigMov IN (select idVendRigMov
                           from (SELECT idVendRigMov
                                 FROM VendRM
                                 WHERE idArtPromo IS NULL AND
                                       (idArtCategoria NOT IN (SELECT idArtCategoria
                                                               FROM VendScontiCatEscl
                                                               WHERE idVendSconti=2) OR 
                                                                     idArtCategoria IS NULL
                                                              ) AN
                                      idVendTesMov=1
                                ) t
                          )

这迫使 MySQL 实例化子查询。实例化后,您不再有在子查询中立即访问外部引用表的问题。

于 2013-06-03T17:58:14.413 回答