如何在Play框架中使用Group By?

时间:2015-02-07 02:41:04

标签: playframework ebean playframework-2.3

我想在播放框架中使用Group By。有人能告诉我如何使用Group By in play框架吗?

2 个答案:

答案 0 :(得分:1)

您可以为ex。

运行 rawsql
String sql   
    = " select order_id, o.status, c.id, c.name, sum(d.order_qty*d.unit_price) as totalAmount"  
    + " from o_order o"   
    + " join o_customer c on c.id = o.kcustomer_id "  
    + " join o_order_detail d on d.order_id = o.id "  
    + " group by order_id, o.status ";  

RawSql rawSql =   
    RawSqlBuilder  
        // let ebean parse the SQL so that it can  
        // add expressions to the WHERE and HAVING   
        // clauses  
        .parse(sql)  
        // map resultSet columns to bean properties  
        .columnMapping("order_id",  "order.id")  
        .columnMapping("o.status",  "order.status")  
        .columnMapping("c.id",      "order.customer.id")  
        .columnMapping("c.name",    "order.customer.name")  
        .create();  


Query<OrderAggregate> query = Ebean.find(OrderAggregate.class);  
    query.setRawSql(rawSql)          
    // add expressions to the WHERE and HAVING clauses  
    .where().gt("order.id", 0)  
    .having().gt("totalAmount", 20);  

List<OrderAggregate> list = query.findList(); 
  

注意:别忘了导入com.avaje.ebean。*;

Source

答案 1 :(得分:0)

GroupBy中使用Ebean的一种方法是使用SqlQuery

// Necessary imports..
import com.avaje.ebean.Ebean;
import com.avaje.ebean.Query;

// SQL query.
String sql = "select max(salary) as maximum_salary, min(salary) as minimum_salary, department_id from employee_details where joining_date > :date group by department_id";

// Will get the list of Sql rows.
List<SqlRow> sqlRows = Ebean.createSqlQuery(sql).setParameter("date", "1995-10-21").findList();

// Getting first row of the list
SqlRow sql_row = sqlRows.get(0);

// Getting values of respective columns by using their name
int maximumSalary = sql_row.getInteger("maximum_salary");
int minimumSalary = sql_row.getInteger("minimum_salary");
int departmentID  = sql_row.getInteger("department_id");