When we distribute data in several tables in the database we make links that indicate some kind of relationship among them.
Normally these relationships are represented by fields we call foreign keys (FK - Foreign Key).
Foreign keys are usually fields that represent primary key from another table (PK - Primary Key).
Through the use of JOIN we use foreign and primary keys to connect two or more tables.
Thinking of your example:
SELECT socio.*, pagamentos.*
FROM socio
INNER JOIN pagamentos ON socio.nrSocio = pagamentos.nrSocio
The fields nrSocio
is primary key in the table socio
and foreign key in the table pagamentos
.
If you want to limit the return of query
to a specific partner we add a clause WHERE
to do this. So:
SELECT socio.*, pagamentos.*
FROM socio
INNER JOIN pagamentos ON socio.nrSocio = pagamentos.nrSocio
WHERE socio.nrSocio = 20
More information and other examples:
How to display data from a foreign key table in my main table?
Category query in Mysql
Query in SQL Server
And how do I make sure when I select the partner, only show the payments from that specific partner in the payments table? and so vice versa, when selecting another partner appear only the same payments... do you understand me? thanks @Emersonjs
– Miguel Henriques
@Miguelhenriques, I don’t think I understand. To return the payments of a specific partner can limit the return with
WHERE
, as described in the example:WHERE socio.nrSocio = 20
. Or is it something else you want?– Emerson JS
I wanted that when selecting a specific partner in the table "socio" return me in the table "payments" the payment data of that selected partner @Emersonjs
– Miguel Henriques
@Miguelhenriques, thinking in terms of instruction SQL you just need to do a query like this:
SELECT * FROM pagamentos WHERE nrSocio = 20
. In this case we would have the payments from partner 20, changing thenrSocio
in the query for another value you would have payments from other partners. Now if we are talking in terms of application, the implementation can vary a lot.– Emerson JS
I do not know if you have received notification of my reply further up, I comment here just to notify you of my explanation further up
– Miguel Henriques
@Miguelhenriques, I get it. Your question is in the implementation of the application and not in the bank consultation, right? As I wrote, change your question with additional information. Also, let me know how you’ve implemented these screens, including even code snippets. You’re using Java, right? Unfortunately, it’s not my area, so we’ll have to rely on the help of other friends. Improving your question becomes easier for others to locate and help you.
– Emerson JS
yes I work with java, yes it is in the implementation, but have to implement the relationship of the two tables in the database
– Miguel Henriques