Counting the quantity of products

Asked

Viewed 71 times

0

Is there any query that returns the quantity of products example I have 10 cell phones, 5 brand a and 5 brand b. This way and not the total count, if yes how to do? Counting them by id.

  • 3

    Detail better the problem , description of the tables , in any case search by COUNT and GROUP BY.

1 answer

5


Let’s take an example: I have the following product table called tb_products:

create table tb_produtos(
 id int(14) not null auto_increment,
 marca varchar(100),
 tipo varchar(100),
 qtd int not null,
 primary key (id)
 );

We now insert some products to make our example:

insert into tb_produtos (marca, tipo, qtd) values ('A', 'CELULAR', 10);
insert into tb_produtos (marca, tipo, qtd) values ('B', 'CELULAR', 15);
insert into tb_produtos (marca, tipo, qtd) values ('A', 'CELULAR', 15);
insert into tb_produtos (marca, tipo, qtd) values ('B', 'CELULAR', 15);
insert into tb_produtos (marca, tipo, qtd) values ('C', 'CELULAR', 45);
insert into tb_produtos (marca, tipo, qtd) values ('A', 'NOTEBOOK', 45);

By selecting our table with the command below we will have the information as per image:

select * from tb_produtos; 

inserir a descrição da imagem aqui

I want to select the quantity per brand of all products where the type is 'CELLULAR'. We execute the command below and we will have result as picture:

select count(marca) as qtd_por_marca, id, marca, tipo, qtd from tb_produtos where tipo = 'CELULAR'
group by marca;

inserir a descrição da imagem aqui

Now I want to know only the quantity of products per brand where they are of type 'CELLULAR' and are of brand A and brand B. We executed the command below and we will have result as image:

select count(marca) as qtd_por_marca, id, marca, tipo, qtd from tb_produtos where tipo = 'CELULAR' AND marca in ('A', 'B')
group by marca;

inserir a descrição da imagem aqui

With Count by id we will have the command/ result in the images below:

select count(id) as qtd_por_marca, id, marca, tipo, qtd from tb_produtos where tipo = 'CELULAR' AND marca in ('A', 'B')
group by marca;

inserir a descrição da imagem aqui

We can still use the command SUM, to add the amount of products we have in each brand in our query, as per command and image below:

select count(id) as qtd_por_marca, id, marca, tipo, SUM(qtd) from tb_produtos where tipo = 'CELULAR' AND marca in ('A', 'B')
group by marca;

inserir a descrição da imagem aqui

The command Count as the name already suggests, is used to count. Already the SUM to add. Now just adapt to your database.

  • 1

    Hello Luiz Fernando, could you put the SQL code in text as well? You can leave the results with image, just put the code because it makes it easier for those who need help.

  • Yes for sure. I did not put the answer not to be so extensive and only the creation code and the Inserts that are not in the answer. I’ll edit, thanks for the dica1

  • I am having a certain difficulty to create a Storage Procedure, that works your tip is of great value and solves the problem but I’m having problems to implement it has how to help me i show what I developed and the tables involved :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.