G
G
Grigory Vasilkov2017-02-24 15:08:03
MySQL
Grigory Vasilkov, 2017-02-24 15:08:03

Is the SUM command (mysql) bound to the SELECT FROM statement?

I ran into a problem that there are a lot of records in the database, and it takes a very long time to display products first, and then filter them by models, then by brands, then by groups.
I went the other way - I first chose the FROM product_group groups, did a LEFT JOIN to brands, then to models, and finally to products, but in SELECT I used the SUM(amount_products) command.
All categories display the same value, which depends on the GROUP BY. That is, I write GROUP BY brand_id, all brands have the same number.
Does this mean that the SUM operator is completely related to the FROM product_group, and therefore simply takes the value of the first row for each of the brands?
How in this case to make a selection of "all brands in which there are no products"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pi314, 2017-02-24
@gzhegow

It's good that the problem has been resolved, but - just for clarity... The operator, more precisely, the aggregating function SUM () is associated with the specified field of the set from which the SELECT is performed, and sums the values ​​of this field for records that have passed all filters (WHERE and HAVING ). In the simplest case, this is one table (then GROUP BY is implied by the primary key, that is, effectively, none at all), but there can be both a JOIN and a subquery, etc. and then, of course, you must explicitly specify GROUP BY, which will refer to one or more fields of THE SAME set. Simply put, aggregating functions refer to the set obtained as a result of a grouping and are applied to those records that were "collapsed" by this grouping.

G
Grigory Vasilkov, 2017-02-24
@gzhegow

Ops, I'm a deer, I forgot to put a link, and he always connected to models without brand, here is the same number and it turns out :( sorry for bothering

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question