1
I own a table that she owns an fk for herself. The point of this is to assemble a history of the previous records, so I have the id and id_previous column, and the previous id_fk for the id column. The problem is that I can’t get more than one record, only one Row.
Example:
table_historico
id | id_anterior
1 | NULL
2 | 1
3 | 2
My attempts were:
SELECT p1.id, p1.id_anterior, p2.id, p2.id_anterior
FROM table_historico p1
LEFT JOIN table_historico p2 ON p1.id_anterior = p2.id
WHERE p1.id = 3
"only an Row" you receive is in the direct query in the bank or in the code? if it is in the code the problem may not be in the
select
(that in my view is correct)– rLinhares
The bank search actually I have the Where condition to specify which accurate record and its previous one. When I inform Where it will bring only 1
– Matheus Hahn
Can’t that be your condition that limits the return to only one?
– rLinhares
Yes, but how do I condition the search to bring the record history I need?
– Matheus Hahn
From what I understand if you consult for
p1.id = 3
it will bring all the records that exist and have 3 asid_anterior
. I don’t know if it can happen in your system of more than one line to be earlier than 3. If yes, I think you will have to give more information about the system; if not, theselect
that is correct.– rLinhares