Answer the question
In order to leave comments, you need to log in
SQL query with summable field for each ID?
Hello
, I got confused with the query that I once wrote, but now I don’t remember sql well. Yes, I used the search, but it was not possible to stumble upon the truth.
There are 2 tables (simplified):
Unit table (id, name, .....,brand)
Brands table (id, name .........)
Unique ID. The unit table stores information about the product, where the brand field stores the brand ID.
The brands table has unique brand names (id, name, etc.).
The goal is to get the structure by request:
brand.id | brand name | COUNT(number of unit.id for the corresponding brand)
Roughly speaking - sum the repetitions:
SELECT brands.name, brands.id FROM unit, brands
WHERE
unit.brand = brands.id
Answer the question
In order to leave comments, you need to log in
SELECT a.id, a.name, count(b.id) as units_cnt
FROM brands a LEFT JOIN unit b ON b.brand = a.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question