2010-05-24 51 views
0

我有一個表叫做發票:MySQL查詢的幫助下,採取總額從一個表,並根據其它表折現值計算gross_total

`si_invoices` (
    `id` int(10) , 
    `biller_id` int(10) , 
    `customer_id` int(10) , 
    `type_id` int(10) , 
    `inv_tax_id` int(10) , 
    `date` date , 
    `unreg_customer` tinyint(1) , 
    `discount` decimal(10,2) , 
    `discount_type` tinyint(1) 
) 

每張發票都有的物品存儲在invoice_items表:

`si_invoice_items` (
    `id` int(10) , 
    `invoice_id` int(10) , 
    `quantity` int(10) , 
    `product_id` int(10) , 
    `warehouse_id` int(10) , 
    `unit_price` decimal(25,2) , 
    `total` decimal(25,2) , 
    `description` text 
) ; 

和稅率表

`si_tax` (
    `tax_id` int(11), 
    `tax_description` varchar(50) , 
    `tax_percentage` decimal(25,6) , 
    `type` varchar(1), 
    `tax_enabled` varchar(1) 
) ; 

這裏是我想要做
第1步:拿到發票項目的sum_total的speciefic發票
第2步:計算折扣,在發票表我有一個discount_type場:
如果等於0,則會有是沒有折扣
如果其等於1,折扣值將被存儲在折扣字段
如果其等於2,折扣sum_total
的百分比

步驟3: CALCULAT Ë基於基礎上的稅號inv_tax_id
的稅,我會看在稅率表,拿到TAX_PERCENTAGE並通過乘以(sum_total - 折扣)

總之這裏是方程
$ gross_total = $ sum_total - $ disount +稅費

+0

我傾向於回答你的問題問題,但後來我發現你從未接受過答案。我迅速看了一眼,似乎至少有一兩個人被標記爲回答。無論如何,我相信有人會爲你回答這個問題,但是當你有機會時,請考慮回顧你以前的問題。 – 2010-05-24 05:15:39

+0

我該如何接受答案?當我看到一個合適的答案時,我點擊向上箭頭,我會檢查看看如何接受答案 – vegatron 2010-05-24 05:39:57

+0

好吧,現在我明白了,點擊箭頭下方的圖像,我接受了我的答案 – vegatron 2010-05-24 05:48:15

回答

0

FINALLY !!!!! 我得到它的工作

這裏是查詢:

 SELECT sum(iv.inv_total - iv.inv_discount + iv.taxes) FROM 
(
SELECT 
     (SELECT sum(ii.total) FROM si_invoice_items ii 
      where ii.invoice_id = v.id) as inv_total , 
     (
      SELECT 
       CASE v.discount_type 
        WHEN 1 THEN v.discount 
        WHEN 2 THEN (v.discount/100) * (SELECT inv_total) 
        ELSE 0 
       END 
     ) AS inv_discount, 

     (SELECT 
      CASE t.type 
       WHEN '$' THEN t.tax_percentage 
       WHEN '%' THEN (t.tax_percentage/100) * (SELECT inv_total - inv_discount) 
       ELSE 0 
      END 
     FROM si_tax as t 
     WHERE t.tax_id = inv_tax_id 
     ) AS taxes 


    FROM 
     si_invoices v 
    ) iv 

優雅,它是一個美麗不是她..:d

感謝阿尼我感謝您的幫助,並邁克佩利它會更好,如果你給出了積極的評論或一點幫助,而不是充當「法律人」

+0

我並不是想扮演「法律人」的角色 - 我只是讓你知道我爲什麼決定繼續前進。回答問題需要一些時間,而且問題比時間更多。不要默默離開,我想我會讓你知道,以免它在未來幫助你獲得更多答案。 – 2010-06-05 16:11:42

0

不知道爲什麼你會想這樣做這一切在MySQL,但這裏是查詢:

SELECT (SELECT `tax_percentage` FROM `si_tax` WHERE `tax_id` = `si_invoices`.`inv_tax_id`) * (`sum_total` - CASE `discount_type` WHEN 1 THEN `discount` WHEN 2 THEN `sum_total` * `discount`/100 ELSE 0 END) AS `gross` 
FROM `si_invoices` 
JOIN (SELECT SUM(`total`) AS `sum_total` FROM `si_invoice_items` WHERE `invoice_id` = `si_invoices`.`id`) AS `t` 
WHERE `id` = ? 
+0

非常感謝Mr.Arnie 我想在mysql中這樣做的原因是因爲我使用phpreports庫,它只接受查詢並執行它並將結果放入xml文件中。(SELECT'tax_percentage' from'si_tax' WHERE'tax_id' ='si_invoices'.'inv_tax_id')*(())*(());(我有一個錯誤,我得到了一個錯誤,所以我改變了它,然後我得到了總數= 0x – vegatron 2010-05-25 19:15:15

+0

SELECT 'sum_total' - CASE'discount_type'當1然後'折扣'當2當然'sum_total' *'折扣'/ 100 ELSE 0 END)作爲'gross' FROM'si_invoices' JOIN(SELECT SUM('total')AS 'sum_total' FROM'si_invoice_items','si_invoices' II,其中'invoice_id' = ii.id)AS't' WHERE'id' = 21 [/代碼] – vegatron 2010-05-25 19:21:09

+0

我已經想出這個 SELECT \t \t(SELECT sum(ii.total)FROM si_invoice_items ii \t \t \t其中ii.invoice_id = v.id)作爲inv_total, \t \t( \t \t \t SELECT \t \t \t \t CASE v.discount_type \t \t \t \t \t WHEN 1 THEN v.discount \t \t \t \t \t WHEN 2 THEN(v.discount/100)*(SELECT inv_total) \t \t \t \t \t ELSE 0 \t \t \t \t END \t \t)AS inv_discount, \t \t(SELECT \t \t \t CASE t.type \t \t \t \t WHEN '$' THEN t.tax_percentage \t \t \t \t WHEN'%'THEN(t.tax_percentage/100)*(SELECT inv_total - inv_di SCOUNT) \t \t \t \t ELSE 0 \t \t \t END \t \t FROM si_tax爲t \t \t WHERE t.tax_id = inv_tax_id \t \t)AS稅費 \t \t,SELECT SUM(inv_total) FROM \t si_invoices v 但我想得到1行! – vegatron 2010-05-26 01:28:00