I have two tables: products
and baskets
, which have the M2M
relation and the table name is basket_products
. I am trying to get the specific basket-related products
total price
and quantity, but I am getting stuck again. How can I fix the following code to get the result? note: I am using the Ent
framework
err = client.Basket.
Query().
Where(basket.ID(2)).
WithProducts().
QueryProducts().
GroupBy("price").
Aggregate(func(selector *sql.Selector) string {
return sql.As(sql.Sum("price"), "price")
}).Scan(ctx, &r)
if err != nil {
log.Println(err)
}
log.Println(r)
The
Aggregate
andGroupBy
functions are the friends. Aggregation | EntI modified the code as given below and got a very optimized
SQL
query, thanks to the Ent team.The generated
SQL
: