2014-11-20 119 views
0

我用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 i的值可以使用在計算service_tax ??

請讓我知道這是可能的

回答

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 
+0

非常感謝你 – Pawan 2014-11-20 21:07:12

+0

我可以初始化sc爲零? (service_charge的值) – Pawan 2014-11-20 21:10:25

+0

@PreethiJain當然你可以'SELECT @sc:= 0 AS service_charge',但不需要將@ sc'初始化爲零,它會計算每一行。您必須確保在設置完成後使用它。 – fthiella 2014-11-20 21:14:14

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