How to bring data from another table related to an id_user (JOIN??)

Asked

Viewed 60 times

0

I have the following tables: Users

inserir a descrição da imagem aqui

And investments

inserir a descrição da imagem aqui

The investment table has the user id. I’m already able to register. How do I show the investments made by a user on the screen? I’m already able to register and have the user id recorded right. My screen will be this below, I will only show the date and value of the investment table:

inserir a descrição da imagem aqui

1 answer

0


I believe the following query sql solve your problem:

SELECT i.created_date, i.valor 
FROM Usuario AS u JOIN Investimento AS i ON u.id = i.id_usuario 
ORDER BY i.created_date;

As you can see in this query I added the ORDER BY just to better organize the query return.

If you want to specify the user id that will be returned the query would be the following:

SELECT i.created_date, i.valor 
FROM Usuario AS u JOIN Investimento AS i ON u.id = i.id_usuario 
WHERE u.id = ?????
ORDER BY i.created_date;

Where "????" is your attribute.

  • 1

    Thanks Lucas! It worked XD

Browser other questions tagged

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