0

我在 Zen Cart 电子商务网站上进行了广泛的工作,并且我对 php 有很好的了解,但只有 SQL 的基本知识。我的问题与我已经涵盖的zen cart或php无关,而是与三个表之间的交互有关。

表 1:inventoryexport -> 列:item_num、item_price、item_stock
表 2:zen_products_supplier -> 列:products_id、australiait_code
表 3:zen_products -> 列(吨,但这里是相关的):products_id、products_price

现在我想做的是通过选择 item_num = australiait_code 的所有产品,用库存导出表中的 item_price 更新 zen_products 表中的“products_price”。

我可以使用此查询选择所需的信息,但我不确定如何将其直接插入到 zen_products 表的更新查询中。

SELECT zen_products_supplier.products_id, inventoryexport.item_price, inventoryexport.item_stock
FROM zen_products_supplier
INNER JOIN inventoryexport
ON inventoryexport.item_num=zen_products_supplier.australiait_code

任何帮助将不胜感激。

4

3 回答 3

0

我通过简单地将 SQL 分为两个步骤来解决我的愚蠢问题。

UPDATE 
inventoryexport ie, zen_products_supplier zps 
SET 
ie.products_id=zps.products_id 
WHERE ie.item_num=zps.australiait_code;

然后我执行以下操作:

UPDATE
zen_products zp, inventoryexport ie 
SET 
zp.products_quantity=ie.item_stock, zp.products_price=ie.item_price 
WHERE 
ie.products_id=zp.products_id;
于 2013-10-04T01:42:41.027 回答
0

这是您主题的好答案: MySQL - UPDATE query based on SELECT Query

可能您必须创建临时表。因为从同一个表中选择更新表可能会出现问题。

于 2013-10-03T11:27:08.880 回答
0

If the requirement is to update the "products_price" in the zen_products table with the total item_price in the inventory export table for all the products where item_num = australiait_code, following query will work:

update zen_products  
set products_price  = (select sum(ie.item_price) 
                       from inventoryexport ie join zen_products_supplier zps on ie.item_num = 
                       zps.australiait_code )

But if you want to update the "products_price" in the zen_products table with the total item_price in the inventory export table for all the products where item_num = australiait_code and also product id in zen_products_supplier = product id of current row being updated in zen_products table, following query will work:

update zp
set zp.products_price = (select sum(ie.item_price)                     
                     from zen_products_supplier zps  
                     join inventoryexport ie on ie.item_num = zps.australiait_code
                     where zps.products_id = zp.products_id)
from zen_products as zp

Hope this helps.

于 2013-10-03T12:33:44.023 回答