How to select and assign the quantity

Asked

Viewed 36 times

0

I’m trying to use mysql to select two tables and assign the number of rows to a field for example

I have two tables: Grupo, Pessoa

I want to make a Join in the Group table showing the number of people assigned to this group through the field Group_id in Person.

The Select that I made:

SELECT u.* , (SELECT COUNT(*) FROM uniforms where u.id) as quantidade from uniforms_group U

but in return the amount of all rows is being assigned to all fields:

inserir a descrição da imagem aqui

1 answer

1


Okay! It was a short while before you hit!

The correct query would be:

SELECT U.* , (SELECT COUNT(*) FROM uniforms u0 where u0.id = U.UserId ) as quantidade from uniforms_group U 

Obs.: Since I couldn’t see your entire relational model, I understood that the column I should place the restriction would be the Userid, If not, we’ll get the comments right.

  • I imagined that this was missing but had succeeded. The relationship is between the group id and the person’s group_id field

Browser other questions tagged

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