我有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...........
谁可以帮助或提供更好的编码技巧?
答案 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
。