0

我有opencart。在产品表中是price我添加了一些额外列的列。

我想填写价格并在价格 > 0 时使用额外数据重新计算它们,我需要在 SQL 中执行此操作。

这就是我想要做的:

IF cost_price > 0 AND shipping_cost1 > 0 AND shipping_cost2 > 0 AND margin > 0
THEN price = cost_price + shipping_cost1 + shipping_cost2_margin
ELSE price = price

据我所知,我可以用CASE. 但是CASE我需要更改priceretail_price. 我需要保留它price

我对查询的想法:

SELECT DISTINCT
    *, pd.name AS name, p.image, m.name AS manufacturer,
    (SELECT price = CASE
        WHEN cost_price > 0 AND shipping_cost >0 AND shipping_cost1 >0 AND margin >0
        THEN ((cost_price * margin) + shipping_cost + shipping_cost1 )
        ELSE (price) FROM product_d.........

原始查询:

SELECT DISTINCT
    *, pd.name AS name, p.image, m.name AS manufacturer,
    (SELECT price FROM product_d...........

谁可以帮助或提供更好的编码提示?

4

1 回答 1

0

您需要调整第二SELECT条语句以修复CASE. 它应该如下所示:

SELECT DISTINCT
    *, pd.name AS name, p.image, m.name AS manufacturer,
    (SELECT 
    CASE
        WHEN cost_price > 0 AND shipping_cost >0 AND shipping_cost1 >0 AND margin >0
        THEN ((cost_price * margin) + shipping_cost + shipping_cost1 )
        ELSE (price)
    END AS price
    FROM product_d.........

您原始查询的这一部分:

SELECT price = CASE

变成

SELECT CASE

此外,添加了倒数第二行:

END AS price

上面添加的行关闭了CASE语句,并为结果列提供了 name price

于 2013-06-24T03:19:06.297 回答