我可以添加然后将此总和放入数据表吗?

时间:2013-04-29 12:45:45

标签: java hibernate

我的代码如下:

this.session = HibernateUtil.getSessionFactory().getCurrentSession();
List<CustomerPayment> paymentList = null;
try {
  org.hibernate.Transaction tx = session.beginTransaction();
  Query q = session.createQuery(
    "from CustomerPayment where DATE like '%" + year + "'");
  paymentList = (List<CustomerPayment> ) q.list();
} catch (Exception e) {
  e.printStackTrace();
}

我从CustomerPayment表中获取此记录。在此表格中,有一个type字段,此类型字段可以包含两个值bankcash

代表:

ID  CODE        DATE        AMOUNT  TYPE
1   CK-00004    11/14/2013  100.5   BANK
3   CK-00004    09/17/2013  250.75  BANK
4   CK-00004    08/20/2013  250.25  CASH

我想要的是将BANKs金额和现金金额加起来并将它们放入数据表中。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

SELECT type ,SUM(amount) FROM CustomerPayment GROUP BY type

会回复你:

  AMOUNT     TYPE
  BANK       370.75
  CASH       250.25