合并它会相当简单,但不确定它会给你有用的价值
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