如何使用计算值来计算另一个

时间:2014-11-20 21:01:29

标签: mysql

我正在使用mysql

我有一个查询,如下所示

Select 
  cost,
  (service_charge*(cost-Discount)) as service_charge,
  (service_tax*(cost-Discount+(service_charge*(cost-Discount)))) as service_tax
From VENDOR_ITEMS
WHERE vendor_items_id = 264

我的问题是我得到service_charge的值我可以在计算service_tax时使用它吗?

如果可能,请告诉我

2 个答案:

答案 0 :(得分:2)

您不能使用计算值来计算另一个,但您可以使用variables

SELECT
  cost,
  @sc := (service_charge*(cost-Discount)) as service_charge,
  (service_tax*(cost-Discount+( @sc ))) as service_tax
FROM
  VENDOR_ITEMS
WHERE
  vendor_items_id = 264

答案 1 :(得分:0)

你必须重复表达

Select cost ,
 (service_charge*(cost-Discount)) as service_charge ,
 (service_tax*(cost-Discount+((service_charge*(cost-Discount))*(cost-Discount)))) as service_tax 
From VENDOR_ITEMS WHERE vendor_items_id = 264