[英]Update table using JOIN and subquery
I'm trying to run an UPDATE that JOINs to other tables, and has a subquery in the WHERE clause. 我正在尝试运行一个将其他表联接起来的UPDATE,并且在WHERE子句中有一个子查询。 However, I'm getting this error:
但是,我收到此错误:
1093 - You can't specify target table 'csi' for update in FROM clause
1093-您无法在FROM子句中指定目标表'csi'用于更新
Here's my query: 这是我的查询:
UPDATE mage_cataloginventory_stock_item csi
INNER JOIN mage_catalog_product_entity cpe
ON csi.product_id = cpe.entity_id
SET csi.Is_in_Stock = 0
WHERE cpe.sku IN (
SELECT cpe.sku
FROM mage_catalog_product_entity cpe
INNER JOIN mage_cataloginventory_stock_item CSI
ON CSI.product_id = cpe.entity_id
INNER JOIN mage_cataloginventory_stock_status CISS
ON CSI.product_id = CISS.product_ID
INNER JOIN mage_catalog_product_entity_int cpei
ON cpe.entity_id = cpei.entity_id
WHERE type_id = 'simple'
AND cpei.attribute_id = 80
AND CSI.Qty = 0 AND cpei.value = 1
)
I see from other sources you can't specify the table being updated in a subquery, but I have no idea how else to do it.. Hoping someone can help point me in the right direction to get this working as I need. 我从其他来源看到,您无法指定要在子查询中更新的表,但是我不知道该怎么做。.希望有人可以帮助我指出正确的方向,以使此功能按我的需要工作。 Thanks!
谢谢!
You could just replace cpe
and CSI
in the sub-query with cpe2
and CSI2
or whichever names you choose. 您可以将子查询中的
cpe
和CSI
替换为cpe2
和CSI2
或您选择的任何名称。
The above would not take the current rows of the tables your updating into account at all. 上面根本不会考虑您要更新的表的当前行。
It could be that this is not what you want. 可能这不是您想要的。 In that case, try:
在这种情况下,请尝试:
UPDATE mage_cataloginventory_stock_item csi
INNER JOIN mage_catalog_product_entity cpe
ON csi.product_id = cpe.entity_id
SET csi.Is_in_Stock = 0
WHERE cpe.sku IN (
SELECT cpe.sku
/* removed joins */
FROM mage_cataloginventory_stock_status CISS
INNER JOIN mage_catalog_product_entity_int cpei
ON cpe.entity_id = cpei.entity_id
WHERE type_id = 'simple'
AND cpei.attribute_id = 80
AND cpei.value = 1
/* moved join condition here */
AND cpe.product_id = CISS.product_ID
AND CSI.Qty = 0
)
Depending on a few things, the below could do the same: (eg cpe.sku
must be unique, otherwise you may be able to get it to work with another join) 取决于几件事,下面的代码可以做同样的事情(例如
cpe.sku
必须是唯一的,否则您可以使它与其他cpe.sku
一起使用)
UPDATE mage_cataloginventory_stock_item csi
INNER JOIN mage_catalog_product_entity cpe
ON csi.product_id = cpe.entity_id
INNER JOIN mage_cataloginventory_stock_status CISS
ON cpe.product_id = CISS.product_ID
INNER JOIN mage_catalog_product_entity_int cpei
ON cpe.entity_id = cpei.entity_id
SET csi.Is_in_Stock = 0
WHERE type_id = 'simple'
AND cpei.attribute_id = 80
AND cpei.value = 1
AND CSI.Qty = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.