通过mysql查询计算利润

时间:2019-01-11 08:12:21

标签: mysql

我的mysql查询如下所示

下面是,否则

SELECT 
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(IF co.retake_flag = 1 THEN
    ((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE 
    ((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
END IF
) as margin FROM
customer_orders co
    LEFT JOIN
m_products p ON p.id = co.product_id
    LEFT JOIN
    orders o on o.id=co.order_id;

下面是案例

SELECT 
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
    ((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE 
    ((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
) as margin FROM
customer_orders co
    LEFT JOIN
m_products p ON p.id = co.product_id
    LEFT JOIN
    orders o on o.id=co.order_id;

我的ID出现错误

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'co.retake_flag = 1 THEN   ((co.product_amt_disc - (p.admin_price+6))/(co.product' at line 7

我遇到CASE错误

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') as margin FROM     customer_orders co         LEFT JOIN     m_products p ON p.' at line 11

我想代表条件重夺标志计算百分比。请帮忙,让我知道是否还有其他需要 谢谢

1 个答案:

答案 0 :(得分:1)

我在CASE语句中缺少END

正确答案如下

SELECT 
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE 
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc) ) END
) as margin
FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;