How to select a table row and display information in another table relative to the table?

0

I am using this code but I am not able to interconnect them as it should be ... I wanted to register Payments (payments) so these only appear when you have to select the partner in the (partner) table ...

        String sql = "SELECT socio.*, pagamentos.* "
                + "FROM socio, pagamentos "
                + "WHERE socio.nrSocio = pagamentos.nrSocio ";

Correction:"Only the * selected payments appear"

Can you see with this explanation? I really need help, thank you

    
asked by anonymous 01.07.2016 / 02:28

1 answer

0

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

    
01.07.2016 / 02:46