跳转至内容

Java 持久性/Ebean/聚合 - 分组/SQL

来自维教科书,为开放世界开放书籍

使用 Ebean.createSqlQuery(sql) 和 SqlRow 的原始 SQL

// using raw sql
String sql = "select order_id, "
           +"        sum(order_qty*unit_price) as total_amount "
           +" from o_order_detail "
           +" where order_qty > :minQty "
           +" group by order_id";

List<SqlRow> sqlRows = 
    Ebean.createSqlQuery(sql)
        .setParameter("minQty",1)
        .findList();

for (SqlRow sqlRow : sqlRows) {
    Integer id = sqlRow.getInteger("order_id");
    Double amount = sqlRow.getDouble("total_amount");

    System.out.println("orderId:" + id + " amt:" + amount);
}
华夏公益教科书