MySQL联盟声明

时间:2013-07-04 13:25:10

标签: mysql

我只想问一下如何加入两个查询 差异是where语句。一个是WHERE c.transac_type = 0,另一个是WHERE c.transac_type = 1"

我希望在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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

您可以获得两种交易类型的总和

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