-2

我有以下Mysql结构:

  • jss_products
    • productID
    • price1
  • jss_extrafields_values
    • exvalID
    • productID
    • extraFieldID
  • jss_extrafields_prices
    • exvalID
    • price1

每个产品都有一些额外字段。我感兴趣的是extraFieldID = 1

我希望使用price1. 我有以下查询,但它只更新每个产品的第一个条目,而不是所有条目。jss_extrafields_pricesjss_products.price1jss_extrafields_price

我正在尝试将价格标准化,jss_extrafields_prices以便对于价格为 的产品20.00,每个相关条目都jss_extrafields_prices变为CURRENTPRICE - 20

那有意义吗?这是我到目前为止所拥有的

UPDATE jss_extrafields_prices AS JEP
INNER JOIN (
    SELECT P.productID, P.price1 AS P1, EP.price1, EP.exvalID FROM jss_products AS P
    INNER JOIN jss_extrafields_values AS EV
    ON P.productID = EV.productID
    INNER JOIN jss_extrafields_prices AS EP
    ON EV.exvalID = EP.exvalID
    WHERE EV.extraFieldID = 1
    GROUP BY P.productID
    ORDER BY P.productID DESC, EP.price1 DESC
) AS X
ON JEP.exvalID = X.exvalID
SET JEP.price1 = JEP.price1 - X.P1

我希望内部查询返回如下内容: productID = 1090 P1 = 20.8333333 price1 = 20.8333333 exvalID = 3236

知道productID1090 有 3 个定价选项,其基本价格是20.83333,然后我想将每个匹配的产品更新jss_extrafields_prices为当前价格减去基本价格。

这有帮助吗?

4

1 回答 1

0

剥离了一些以前类过于本地化的信息,这简化为带有 INNER JOIN 的 UPDATE:

UPDATE jss_extrafields_prices 
INNER JOIN (
    SELECT P.productID, P.price1 AS baseprice, JEP.price1 AS optionprice, JEP.exvalID, (JEP.price1 - P.price1) AS adjustedprice FROM jss_products AS P
    INNER JOIN jss_extrafields_values AS JEV
    ON P.productID = JEV.productID
    INNER JOIN jss_extrafields_prices AS JEP
    ON JEV.exvalID = JEP.exvalID
    WHERE JEV.extraFieldID = 1
) AS X
ON jss_extrafields_prices.exvalID = X.exvalID
SET jss_extrafields_prices.price1 = X.adjustedprice
于 2013-01-23T10:02:17.090 回答