我有以下数据库结构:
[Table `lists`]
one has many [Table `lists_positions`]
one has one [Table `products`]
many has many through [Table `products_prices_tables`] [Table `prices_tables`]
has many [Table `prices`]
是否有可能获得所有产品的总和'数据库中所有现有清单中的价格?
我尝试过:
SELECT x.list_id AS id, x.modified_on AS modified_on, ROUND(SUM(x.sum_price), 2) AS cart_price
FROM (
SELECT list_id, modified_on, AVG(y.price_weight) AS sum_price
FROM (
SELECT l.id AS list_id, l.modified_on AS modified_on, pr.price_weight AS price_weight
FROM `lists_positions` lp
INNER JOIN `lists` l ON l.id = lp.list_id
INNER JOIN `products` p ON p.id = lp.product_id
LEFT JOIN `products_prices_tables` ppt ON ppt.product_id = p.id
LEFT JOIN `prices_tables` pt ON pt.id = ppt.price_table_id
LEFT JOIN `prices` pr ON pr.price_table_id = pt.id
GROUP BY l.id
) y
GROUP BY y.list_id
) x
GROUP BY x.list_id
但它似乎不起作用。
提前致谢。
编辑:这似乎对我有用:
SELECT modified_on, ROUND(SUM(y.price_weight), 2) AS sum_price
FROM (
SELECT DATE(l.modified_on) AS modified_on,
(AVG(pr.price_weight) * lp.amount) AS price_weight
FROM `lists_positions` lp
INNER JOIN `lists` l ON l.id = lp.list_id
INNER JOIN `products` p ON p.id = lp.product_id
INNER JOIN `users` u ON u.id = l.user_id
INNER JOIN `products_prices_tables` ppt ON ppt.product_id = p.id
INNER JOIN `prices_tables` pt ON pt.id = ppt.price_table_id
INNER JOIN `prices` pr ON pr.price_table_id = pt.id
WHERE (
(:sexCriteria) AND (:rangeCriteria)
) GROUP BY lp.id
) y
GROUP BY y.modified_on