SQL queries with multiple tables

Asked

Viewed 40 times

-1

I am studying SQL and needed to make a query, which for me is more complex.

I have the following tables:

Tabela Ativo: id, tipo_ativo
Tabela Carteira: id, nome..
Tabela Movimento: id, ativo_id, carteira_id

I need to return a list of "Wallets" who possess "Movements" with "Assets" of a certain type, which would be defined in the WHERE clause.

What would this consultation look like? Please help me

  • There are already many posts about this on the site, search for SQL and JOIN. Example: https://answall.com/q/6441, more links in the answer here, https://answall.com/q/267808 and much more.

1 answer

-1


You can use the following query structure:

   select * from Carteira 
        inner join  Movimento on Carteira.id = Movimento.carteira_id
        inner join  Ativo  on Ativo.id = Movimento.ativo_id

NOTE: You have to be aware that if the Portfolio, Movement and Active tables have an attribute with the same name the (*) asterisk cannot be used in the query. Being necessary to qualify the names of the attributes you want to project in the query

Explaining the query

 inner join  Movimento on Carteira.id = Movimento.carteira_id 

Sets the relationship attribute between the Movement and Portfolio tables to be the id attributes of the Portfolio and the Movement Portfolio Attribute

inner join  Ativo  on Ativo.id = Movimento.ativo_id

Sets the relationship attribute between the Movement and Active tables to be the id attributes of the Active table and the active attribute.

From what I understand the table Movement is a relationship entity. This way it is necessary to establish this structure to bring the relationship of Assets with Movements.

Browser other questions tagged

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