0
I have the following SELECT
to select data from a table (chat
) in a chat system:
SELECT * FROM (SELECT * FROM chat WHERE id_chat = '$chat_id' ORDER BY id DESC LIMIT 10) S WHERE id_chat = '$chat_id' ORDER BY id ASC LIMIT 10
The same makes the query returning the last ten messages to display them in decreasing form.
In this table (chat
), there’s a column usuario
where it represents the id
of the user who sent the message.
I would like to, from the id
of the user who sent the message, be able to return the data of such user (table usuarios
).
Table example usuarios
:
id | nome | foto
1 | Lucas | perfil.jpg
What is the best way to do this using the above query? LEFT JOIN
? INNER JOIN
? And, how to do?
I believe that using the example of
INNER JOIN
will work, since there will always be a link between the two tables (because I will have to consult the data of the user who sent the chat, and this user will always be present in both tables), however, how to adapt this solution to myquery
existing (present in the question), bearing in mind that I will have to return the last ten messages in descending order?– Igor
@Igor I don’t understand why you’re wearing a two
querys
, and you can use only thesubQuery
, to adapt myquery
only changes theSELECT *
and addLIMIT 10
.– Guilherme Lautert