我在MySQL数据库中有两个表。
客户下达存储在order_item
表格中的产品订单 - 从product
到order_item
的一对多关系。
目前,我正在执行以下查询。
SELECT t0.prod_id,
sum(t1.quantity_ordered)
FROM projectdb.product t0,
projectdb.order_item t1
WHERE (t0.prod_id = t1.prod_id)
GROUP BY t0.prod_id
HAVING (sum(t1.quantity_ordered) >= ?)
ORDER BY sum(t1.quantity_ordered) DESC
生成此SQL的Criteria查询如下所示。
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]>criteriaQuery=criteriaBuilder.createQuery(Object[].class);
Metamodel metamodel = entityManager.getMetamodel();
Root<OrderItem> root = criteriaQuery.from(metamodel.entity(OrderItem.class));
Join<OrderItem, Product> orderItemProdJoin = root.join(OrderItem_.prodId, JoinType.INNER);
List<Expression<?>>expressions=new ArrayList<Expression<?>>();
expressions.add(orderItemProdJoin.get(Product_.prodId));
expressions.add(criteriaBuilder.sum(root.get(OrderItem_.quantityOrdered)));
criteriaQuery.multiselect(expressions.toArray(new Expression[0]));
criteriaQuery.groupBy(orderItemProdJoin.get(Product_.prodId));
criteriaQuery.having(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.sum(root.get(OrderItem_.quantityOrdered)), criteriaBuilder.literal(5)));
criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(root.get(OrderItem_.quantityOrdered))));
List<Object[]> list = entityManager.createQuery(criteriaQuery).getResultList();
此查询汇总了order_item
表中每组产品的数量。
它显示的行列表如下所示。
prod_id qunatity_ordered
6 11
8 8
26 8
7 7
31 7
12 6
27 6
24 5
9 5
是否可以只计算此查询生成的行数 - 在这种情况下为9?
我正在使用EclipseLink 2.5.2和Hibernate 4.3.6 final提供的JPA 2.1。
答案 0 :(得分:0)
您有两种选择:
SELECT COUNT(*)
FROM (
SELECT 1,
FROM projectdb.product t0,
projectdb.order_item t1
WHERE (t0.prod_id = t1.prod_id) /* I prefer not to use Implicit Joins */
GROUP BY t0.prod_id
HAVING (sum(t1.quantity_ordered) >= ?)
) groups
OR:
list.size();
答案 1 :(得分:0)
计算此类行的一种方法是将给定查询包装在另一个计算行的查询中,并使给定查询成为子查询,如下所示。
SELECT count(DISTINCT(t0.prod_id))
FROM projectdb.product t0
WHERE EXISTS (SELECT t1.prod_id
FROM projectdb.order_item t2,
projectdb.product t1
WHERE ((t1.prod_id = t0.prod_id )
AND ( t1.prod_id = t2.prod_id))
GROUP BY t1.prod_id
HAVING (sum(t2.quantity_ordered) >= ?))
生成上述SQL的条件查询。
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Long>criteriaQuery=criteriaBuilder.createQuery(Long.class);
Metamodel metamodel = entityManager.getMetamodel();
Root<Product> root = criteriaQuery.from(metamodel.entity(Product.class));
criteriaQuery.select(criteriaBuilder.countDistinct(root));
Subquery<Long> orderItemSubquery = criteriaQuery.subquery(Long.class);
Root<OrderItem> orderItemRoot = orderItemSubquery.from(metamodel.entity(OrderItem.class));
Join<OrderItem, Product> orderItemProdJoin = orderItemRoot.join(OrderItem_.prodId, JoinType.INNER);
orderItemSubquery.select(orderItemProdJoin.get(Product_.prodId));
orderItemSubquery.where(criteriaBuilder.equal(root, orderItemRoot.get(OrderItem_.prodId)));
orderItemSubquery.groupBy(orderItemProdJoin.get(Product_.prodId));
orderItemSubquery.having(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.sum(orderItemRoot.get(OrderItem_.quantityOrdered)), criteriaBuilder.literal(5)));
criteriaQuery.where(criteriaBuilder.exists(orderItemSubquery));
Long count = entityManager.createQuery(criteriaQuery).getSingleResult();
System.out.println("count = "+count);
我通常避免使用IN()
子查询并使用EXISTS()
子查询。不过,可以使用IN()
重写相同的查询,如下所示。
SELECT count(DISTINCT(t0.prod_id))
FROM projectdb.product t0
WHERE t0.prod_id IN (SELECT t1.prod_id
FROM projectdb.order_item t2,
projectdb.product t1
WHERE (t1.prod_id = t2.prod_id)
GROUP BY t1.prod_id
HAVING (sum(t2.quantity_ordered) >= ?))
相应的标准查询。
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Long>criteriaQuery=criteriaBuilder.createQuery(Long.class);
Metamodel metamodel = entityManager.getMetamodel();
Root<Product> root = criteriaQuery.from(metamodel.entity(Product.class));
criteriaQuery.select(criteriaBuilder.countDistinct(root));
Subquery<Long> orderItemSubquery = criteriaQuery.subquery(Long.class);
Root<OrderItem> orderItemRoot = orderItemSubquery.from(metamodel.entity(OrderItem.class));
Join<OrderItem, Product> orderItemProdJoin = orderItemRoot.join(OrderItem_.prodId, JoinType.INNER);
orderItemSubquery.select(orderItemProdJoin.get(Product_.prodId));
orderItemSubquery.groupBy(orderItemProdJoin.get(Product_.prodId));
orderItemSubquery.having(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.sum(orderItemRoot.get(OrderItem_.quantityOrdered)), criteriaBuilder.literal(5)));
criteriaQuery.where(criteriaBuilder.in(root.get(Product_.prodId)).value(orderItemSubquery));
Long count = entityManager.createQuery(criteriaQuery).getSingleResult();
System.out.println("count = "+count);
关于ORM的限制,我找不到比这更好的选择。