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

Asked

Viewed 448 times

0

I’m using this code, but I’m not connecting it properly... I would like to register Payments (payments) for these to appear only when selecting the partner in the table(socio)...

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

inserir a descrição da imagem aqui

Fix: "only show payments* from selected"

Can you understand that? I really need help, thank you

1 answer

0

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

  • @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?

  • 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

  • @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 the nrSocio 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.

  • 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

  • @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.

  • yes I work with java, yes it is in the implementation, but have to implement the relationship of the two tables in the database

Show 2 more comments

Browser other questions tagged

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