Problem sorting data resulting from Inner Join

Asked

Viewed 35 times

0

Guys, I have a basic question but I’m having a huge difficulty in making an appointment.

I have a protocol system in which protocol data is recorded in a table called PROTOCOL and the updates of each protocol are in another table called SITUATION.

I have an APB that shows all protocols that are 7 days without movement, follow my query:

SELECT p.ID_PROTOCOLO, p.NUMERO, s.DATA_CADASTRO
FROM protocolo p
INNER JOIN situacao s ON s.ID_PROTOCOLO = p.ID_PROTOCOLO
where s.DATA_CADASTRO < DATE_ADD(CURDATE(), INTERVAL -7 day)
GROUP BY p.ID_PROTOCOLO

I need this INNER JOIN to bring the last (most current) SITUATION registration but it is always bringing the first registered (oldest).

I already tried to do a sub-visit and I tried to do a select inside INNER JOIN, and I did not succeed.

For those who have experience just seeing will already know the solution, but for me it is very difficult to solve this small problem. Thanks for your help.

1 answer

2

Have you tried using the ORDER BY s.DATA_CADASTRO DESC?

Browser other questions tagged

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