Connection between 3 tables to rescue information, sql

Asked

Viewed 55 times

0

I have 3 tables with this structure

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

I tried but could only enter two tables, but I would need it to be in the 3 to "save/clean" code.

it is necessary to enter the table helpcenter_topics catch a few 20 more topics recentemente inseridos, catch the username of who posted taking into account that in the table helpcenter_topics the column uid represents the idu associative that the league in the table users and also catch the username of the last person who posted an answer on that topic I take into account that the tabla topics_answers is where you keep the answers and that the topic_id is the id associative that connects to helpcenter_topics

how to do this in a single line of code?

1 answer

1


One remark, you’re not saving the response time so you have no way of knowing who the last one was. Try this:

SELECT ht.title, upost.username, 
       (SELECT ureply.username FROM topics_answers ta
        INNER JOIN users ureply on ureply.idu = ta.uid
        WHERE ta.topic_id = ht.id /* filtra somente do tópico atual */
        ORDER BY ta.id DESC /* resposta mais recente */
        LIMIT 1)
FROM helpcenter_topics ht
INNER JOIN users upost on upost.idu = ht.uid
LIMIT 20

Browser other questions tagged

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