【问题标题】:Multiple Select Statement in MariaDB MySQLMariaDB MySQL 中的多选语句
【发布时间】:2017-08-07 05:17:53
【问题描述】:

我有以下 sql 语句,并且它在一定程度上可以正常工作,如果可能的话,我希望让它变得更好,并在最后一行获得所有选择的总数。我还尝试在一个语句中创建 WHERE,但无法返回我在下面完成的订单。

感谢和问候

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)

【问题讨论】:

  • 尝试重新组织您的数据,以便您可以使用 '=' 而不是 'LIKE'
  • order by case when account LIKE 'INC%' then 1 when ... when account LIKE 'IDEX%' then 5 end

标签: mysql sql select


【解决方案1】:

您可以将 OR 与多个 Like 运算符一起使用

  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

【讨论】:

猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2014-08-26
  • 2013-06-29
相关资源
最近更新 更多