2017-08-07 89 views
-1

我有以下的sql語句,它的工作很好,在一定程度上,如果可能的話,希望使它更好,並且總計得到所有在最後一行的選擇。我也嘗試在一個語句中創建WHERE,但無法返回我在下面完成的命令。MariaDB中的多重選擇語句MySQL

感謝&問候

SELECT 
    coalesce(account, ' ') AS Account, 
    sum(gl.credit - gl.debit) AS Balances 
    FROM `tabGL Entry` AS gl 
    WHERE account LIKE 'INC%' 
    GROUP BY account WITH ROLLUP 
UNION ALL 
SELECT 
    coalesce(account, ' ') AS Account, 
    sum(gl.debit - gl.credit) AS Balances 
    FROM `tabGL Entry` AS gl 
    WHERE account LIKE 'DCOI%' 
    GROUP BY account WITH ROLLUP 
UNION ALL 
SELECT 
    coalesce(account, ' ') AS Account, 
    sum(gl.debit - gl.credit) AS Balances 
    FROM `tabGL Entry` AS gl 
    WHERE account LIKE 'DMC%' 
    GROUP BY account WITH ROLLUP 
UNION ALL 
SELECT 
    coalesce(account, ' ') AS Account, 
    sum(gl.debit - gl.credit) AS Balances 
    FROM `tabGL Entry` AS gl 
    WHERE account LIKE 'INFC%' 
    GROUP BY account WITH ROLLUP 
UNION ALL 
SELECT 
    coalesce(account, ' ') AS Account, 
    sum(gl.debit - gl.credit) AS Balances 
    FROM `tabGL Entry` AS gl 
    WHERE account LIKE 'IDEX%' 
    GROUP BY account WITH ROLLUP 

爲了澄清結果所需

Inc item 1 ---- 100 
Inc item 2 ---- 100 
Inc Total ---- 200 
DCOI item 1 ---- 100 
DCOI item 2 ---- 100 
DCOI Total ---- 200 
DMC item 1 ---- 100 
DMC item 21 ---- 100 
DMC Total ---- 200 
Total Inc-(DCOI+DMC+INFC+IDEX) 
+0

嘗試重新組織數據,以便您可以使用'='而不是'LIKE' – Strawberry

+0

'按賬號LIKE'INC%'排序ñ1時......當帳戶LIKE'IDEX%'然後5結束' – jarlh

回答

-1

您可以使用或與多個像運營商

SELECT 
     coalesce(account, ' ') AS Account, 
     sum(gl.credit - gl.debit) AS Balances 
     FROM `tabGL Entry` AS gl 
     WHERE 
     account LIKE 'INC%' 
     OR LIKE 'DCOI%' 
     OR account LIKE 'DCOI%' 
     OR account LIKE 'DMC%' 
     OR account LIKE 'INFC%' 
     OR account LIKE 'IDEX%' 
     GROUP BY account WITH ROLLUP 
+0

感謝您的答案,我其實也嘗試過,當我得到的結果它不是我需要它的順序。 – Said

+0

https://stackoverflow.com/questions/3609166/mysql-order-by-like-like這個答案可能有幫助 –

+0

如果這是行得通的,那就需要多一點思考 – Strawberry