Tables of MYSQL

Asked

Viewed 31 times

-2

inserir a descrição da imagem aqui

As the image shows, I have the table adms_unidade where I can already show the units listed, and I have the table adms_equipamentos with various registered equipment.

I want to show how many registered equipment I have in each unit using the adms_unidade_id which is the foreign key to tablea adms_equipamentos key-related primary table adms_unidades.

I’ve seen some examples and ways, but I don’t know if it’s typo or not.

  • Make an INNER JOIN of the tables and use the COUNT aggregation function with the GROUP BY clause. Posting images in place of codes is not an attitude well seen on this site, the same not to post the code of the query you tried with the explanation of why you consider the wrong result.

1 answer

0

Good night,

just make the table link adms_equipamentos with the adms_unidades with left outer join, in case there is no equipment registered for that unit, and then you group by unit and count how many records there are for each grouping.

select au.nome_da_unidade, count(*) qtd_equipamentos
from adms_unidades au
left outer join adms_equipamentos ae
on ae.adms_unidade_id = au.id
group by au.nome_da_unidade

Browser other questions tagged

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