Answer the question
In order to leave comments, you need to log in
How to compose Mysql query with grouping and cell sums in WHERE clause?
Please tell me how to correctly compose a Mysql query, I'm not good at compound queries.
I have a request
SELECT `products`.`name` AS `name`,
SUM(`part`.`rest`) AS `rest`, -- остаток
SUM(`part`.`reserve`) AS `reserve`, -- резерв
`part`.`revision_actually` AS `rev_actually`, -- актуальный остаток (в админке менеджеры указывают)
`part`.`code` AS `code`, -- код товара
`part`.`point_id` AS `point_id` -- точка (магазин) к которой привязана партия
FROM `products` -- товары
INNER JOIN `part` -- партии
ON `products`.`id` = `part`.`prod_id`
WHERE `part`.`point_id` IN(1,2) AND (`part`.`rest` + `part`.`reserve`) != `part`.`revision_actually`
GROUP BY `part`.`code`, `part`.`point_id`
ORDER BY `part`.`code` ASC
SELECT `products`.`name` AS `name`,
SUM(`part`.`rest`) AS `rest`, -- остаток
SUM(`part`.`reserve`) AS `reserve`, -- резерв
`part`.`revision_actually` AS `rev_actually`, -- актуальный остаток (в админке менеджеры указывают)
`part`.`code` AS `code`, -- код товара
`part`.`point_id` AS `point_id` -- точка (магазин) к которой привязана партия
FROM `products` -- товары
INNER JOIN `part` -- партии
ON `products`.`id` = `part`.`prod_id`
WHERE `part`.`point_id` IN(1,2) AND (SUM(`part`.`rest`) + SUM(`part`.`reserve`)) != `part`.`revision_actually`
GROUP BY `part`.`code`, `part`.`point_id`
ORDER BY `part`.`code` ASC
Answer the question
In order to leave comments, you need to log in
HAVING
in where you specify the conditions that apply to data before grouping, in having - after grouping
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question