2011-12-21 61 views
0

如何通過使用先前公式的引用來減少嵌套公式的長度?MySQL - 在select語句中減少嵌套公式的長度

比如我想做到以下幾點,但得到一個錯誤:

(orders.size_0 + orders.size_1 + orders.size_2 + orders.size_3 + orders.size_4 +  orders.size_5 + orders.size_6) AS tot_qty, 
ROUND(tot_qty*orders.order_price,0) AS tot_price, 
ROUND(tot_price/f.rate,0) AS price_gbp 

感謝, 德里克。

+1

問題是泥 – 2011-12-21 07:36:59

+0

一樣清晰?像這樣混淆是很好的。 – Interrobang 2011-12-21 07:49:54

回答

1

您可以使用子查詢,例如 - 你什麼錯誤

SELECT tot_qty, tot_price, ROUND(tot_price/sub1.rate,0) AS price_gbp FROM (
    SELECT tot_qty, ROUND(tot_qty * sub2.order_price, 0) AS tot_price FROM (
    SELECT (orders.size_0 + orders.size_1 + orders.size_2 + orders.size_3 + orders.size_4 + orders.size_5 + orders.size_6) AS tot_qty FROM table 
     ... 
    ) sub2 
) sub1