2013-07-04 166 views
0

我只想問如何加入兩個查詢,只有 區別在哪裏聲明。一個是WHERE c.transac_type = 0,另一個是WHERE c.transac_type = 1"MySQL聯盟聲明

我想在WHERE c.transac_type = 0和WHERE c.transac_type = 1的條件下看到SUM(c.amount)的輸出,因爲它們具有不同的SUM(c.amount)輸出。

SELECT a.id, a.branch_code, SUM(c.amount), d.category, e.branch_commission 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type = 0 
GROUP BY d.category, b.branch_code_id ORDER BY SUM(c.amount) DESC 
+3

爲什麼不'WHERE c.transac_type IN( 0,1)'而不是'UNION'? –

+0

mysqli在這裏是一個錯誤的標籤 –

+0

只是FYI,術語'[mysql]和'正確加入''目前出現在SO 952次。與出現22935次的[mysql]和「left join」相比...我只是說。 – Strawberry

回答

1

,那麼你要使用條件彙總:

SELECT a.id, a.branch_code, SUM(c.amount) as totAmount, 
     sum(case when c.transac_type = 0 then c.amount end) as Trans0_Amount, 
     sum(case when c.transac_type = 1 then c.amount end) as Trans1_Amount, 
     d.category, e.branch_commission 
FROM control_panel_client_create AS a 
RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
WHERE c.transac_type IN (0, 1) 
GROUP BY d.category, b.branch_code_id 
ORDER BY SUM(c.amount) DESC 
+0

OMG!這正是我所需要的。謝謝您的幫助。已經使用它:)並完美的作品:) – Yassi

0

UNIONing這將是相當簡單的,但不知道它會給你有用的值

SELECT a.id, a.branch_code, SUM(c.amount) AS AmountSum, d.category, e.branch_commission 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type = 0 
GROUP BY d.category, b.branch_code_id 
UNION 
SELECT a.id, a.branch_code, SUM(c.amount) AS AmountSum, d.category, e.branch_commission 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type = 1 
GROUP BY d.category, b.branch_code_id 
ORDER BY AmountSum DESC 

你可以只讓和兩個TRANSAC類型

SELECT a.id, a.branch_code, SUM(c.amount) AS AmountSum, d.category, e.branch_commission 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type IN (0, 1) 
GROUP BY d.category, b.branch_code_id 
ORDER BY AmountSum DESC 

或者總和對於兩者分開

SELECT a.id, a.branch_code, SUM(c.amount) AS AmountSum, d.category, e.branch_commission , c.transac_type 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type IN (0, 1) 
GROUP BY d.category, b.branch_code_id, c.transac_type 
ORDER BY AmountSum DESC 
把這兩方面都在同一條線上的

討厭的方式: -

SELECT a.id, a.branch_code, SUM(IF(c.transac_type=0,c.amount, 0)) AS AmountSumTransac_0, SUM(IF(c.transac_type=1, c.amount, 0)) AS AmountSumTransac_1, d.category, e.branch_commission 
FROM control_panel_client_create AS a 
    RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
    RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
    RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
    INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
    WHERE c.transac_type IN (0, 1) 
GROUP BY d.category, b.branch_code_id 
ORDER BY AmountSum DESC 
+0

我希望在WHERE c.transac_type = 0和WHERE c.transac_type = 1的條件下看到SUM(c.amount)的輸出,因爲它們具有不同的SUM輸出(c.amount) - – Yassi

+0

最後一個查詢應該執行(將c.transac_type添加到GROUP BY,並將其檢查更改爲IN)。 – Kickstart

+0

是否有可能我將WHERE c.transac_type = 0的SUM(c.amount)和WHERE的另一個列(c.amount)分隔爲一列c.transac_type = 1 – Yassi

1

你可以使用IN(0,1),而不是工會。如果你想在兩個不同的列交易類型的款項

試試這個

SELECT a.id, a.branch_code, SUM(c.amount), d.category, e.branch_commission 
FROM control_panel_client_create AS a 
RIGHT JOIN sales_add_h AS b ON a.id = b.branch_code_id 
RIGHT JOIN sales_add_i AS c ON b.id = c.sales_h_id 
RIGHT JOIN control_panel_item_create AS d ON c.item_code_id = d.id 
INNER JOIN control_panel_client_create AS e ON b.branch_code_id = e.id 
WHERE c.transac_type IN (0, 1) 
GROUP BY d.category, b.branch_code_id,c.transac_type ORDER BY SUM(c.amount) DESC 
+0

我想在WHERE c.transac_type = 0和WHERE c.transac_type = 1的條件下看到SUM(c.amount)的輸出,因爲它們有SUM(c.amount)不同的輸出 – Yassi

+0

然後通過檢查我更新的答案在組中添加'c.transac_type' –

+0

我可以將WHERE c.transac_type = 0的SUM(c.amount)列和另一個列的SUM(c.amount)爲WHERE c.transac_type = 1。 – Yassi