重新计算表格价格而不更改表格名称

时间:2013-06-24 02:36:52

标签: sql case

我有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我需要将price更改为retail_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...........

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

1 个答案:

答案 0 :(得分: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语句,并且还会为结果列指定名称price