1

我创建了这个问题,女巫现在得到了照顾,但我在同一个查询中遇到了另一个问题:

SELECT
  t.type,
  SUM( t.external_account )
FROM
  u_contracts c,
  u_data u,
  u_transactions t
WHERE
  c.user_id = u.id
  AND t.contract_id = c.id
  AND t.nulled =0
  AND DATE (c.init_date) < DATE (u.dead)
  AND u.dead IS NOT NULL
  AND t.type != 'repay'
GROUP BY
  t.type
ORDER BY
  FIELD( t.type, 'initial', 'comission', 'overpay', 'penalty', 'penalty2' );

我得到的结果如下:

+-----------+---------------------------+
| type      | SUM( t.external_account ) |
+-----------+---------------------------+
| prolong   |                 360560.00 |
| reg       |                   3889.00 |
| reg2      |                    301.20 |
| initial   |                 610628.54 |
| comission |                 125623.49 |
| overpay   |                   6461.57 |
| penalty   |                  21461.52 |
| penalty2  |                   4010.00 |
+-----------+---------------------------+

我需要总结类型为prolong + reg + reg2的结果并将它们添加到列表的末尾。

现在我完全不知道它们是如何求和的,考虑到它们是GROUP BY t.type请求的结果。

4

1 回答 1

3

试试这个:

SELECT 
    t.type,
    SUM(t.external_account)
FROM
(
    SELECT
      CASE t.type
        WHEN 'prolong' THEN 'prolong + reg + reg2'
        WHEN 'reg' THEN 'prolong + reg + reg2'
        WHEN 'reg2' THEN 'prolong + reg + reg2'
        ELSE t.type
      END AS type,
      t.external_account
    FROM
      u_contracts c,
      u_data u,
      u_transactions t
    WHERE
      c.user_id = u.id
      AND t.contract_id = c.id
      AND t.nulled =0
      AND DATE (c.init_date) < DATE (u.dead)
      AND u.dead IS NOT NULL
      AND t.type != 'repay'
) t 
GROUP BY
      t.type
ORDER BY
  FIELD( t.type, 'initial', 'comission', 'overpay', 'penalty', 'penalty2' );
于 2012-10-03T12:04:58.320 回答