When we distribute data in multiple tables in the database we make links that indicate some type of relationship between them.
Usually these relationships are represented by fields that we call foreign keys FK - Foreign Key ).
Foreign keys are usually fields that represent primary key of another table .
Using JOIN operations we use foreign and primary keys to link two or more tables.
Thinking about your example:
SELECT socio.*, pagamentos.*
FROM socio
INNER JOIN pagamentos ON socio.nrSocio = pagamentos.nrSocio
The nrSocio
field is the primary key in the socio
table and the foreign key in the pagamentos
table.
If you want to limit the return of query
to a specific partner we add a WHERE
clause 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?
MySQL Category Query
SQL Server Query