有一张table CART_ITEMS
,在哪里 drink_id
是 id
喝了一杯,然后 order_id
是 id
它所在的顺序, count
是订单上的饮料量。你需要展示这张table上6种最受欢迎的饮料。我编写了sql查询:
SELECT drink_id, sum(count) as count
FROM cart_items
GROUP BY drink_id
ORDER BY 2 DESC
LIMIT 6
但是我们怎么能用英语重写呢 HQL
现在?我试着写代码,但intelij的想法对 sum
以及 limit
功能。
@Query("select C.drink, sum(count) as count from CartItem C group by C.drink order by 2 DESC limit 6")
List<Drink> getMostPopularDrinks();
购物车项目:
@Getter
@Setter
@Entity
@NoArgsConstructor
@Table(name = "cart_items")
public class CartItem {
@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public static class CartId implements Serializable {
private Long order_id;
private Long drink_id;
}
/**
* key @EmbeddedId
*/
@EmbeddedId
private CartId cartId;
@ManyToOne
@JoinColumn(name = "order_id", referencedColumnName = "id",
nullable = false, insertable = false, updatable = false)
private Order order;
@ManyToOne
@JoinColumn(name = "drink_id", referencedColumnName = "id",
nullable = false, insertable = false, updatable = false)
private Drink drink;
/**
* Count of drink
*/
private int count;
}
暂无答案!
目前还没有任何答案,快来回答吧!