Make a select with 3 tables and return in table 3 the id records of the first

Asked

Viewed 201 times

-1

I have three tables, the first is occurrence, the second is to individuals and third vehicles. The individual tables and vehicles I made occurrences subformularios. I have the first key in the occurrence id_occurrence, the other tables I have the primary key of them and the foreign keys linked to occurrences. I want in the table vehicles that have a field that shows the names of the individuals related to the occurrence, have to be the individuals who have just been registered or is the same occurrence (id).

  • uses Inner Join to relate the two tables by id (primary key of the main key with the foreign key of the daughter), selecting the fields you want...

  • http://www.bosontreinamentos.com.br/mysql/mysql-inner-join-consultar-dados-em-duas-ou-mais-tabelas-29/

1 answer

0

According to the comment:

SELECT i.nome, MAX(o.id_ocorrencia) FROM individuo i
LEFT JOIN ocorrencia as o ON i.id_ocorrencia_ind = o.id_ocorrencia
GROUP BY i.nome;
  • I did it the way I said, but it’s returning all the records of the field I want from table 2, what I wanted for example, the person type table 1(main) and then table 2 (son) in the child has a name field, that I wanted to appear in a field in table 3 one or all digital names in table 2 related to the last id. Maybe it would be some relationship with last id. thanks Luiz

  • I don’t understand, give me the model E tbm not understand what you want, use the sqlfiddle.com and give me the link I try to help you.

  • Thank you Luiz I will put in sqlfiddle.com, but just to be clear, I have 3 tables, the first is occurrence, second is to individuals and third vehicles. The individual tables and vehicles I made occurrences subformularios. I have the first key in the occurrence id_occurrence, the other tables I have the primary key of them and the foreign keys linked to occurrences. I want in the table vehicles that have a field that shows the names of the individuals related to the occurrence, have to be the individuals who have just been registered or is the same occurrence (id).

  • Luiz Sérgio follows the link of the bank model I made, I thank him immensely. https://drive.google.com/file/d/1TaSb7IsnNEUfY-Ht22KQ_Hoz9AyklW3Y/view?usp=sharing

  • Edit the question with the description two msgs above and the image just above, which I answer

  • For the better understanding

  • Gave this error:Query error (1054): Unknown column 'o. id_occurrence' in 'having clause'

  • I pulled the havinbg from the consult. Try again.

  • No error, but shows the occurrence ids with their respective names, and not only the current id (last id of the occurrence of the bank with its respective name). Thank you Luiz

  • tried this way: . The id seems the last but the name does not.

Show 5 more comments

Browser other questions tagged

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