Using select in where where

I have a table called message. I try to select a message from a table only when man_send_id and person_receive_id are different.
I can choose person_send_id and person_receive_id correctly.
However, I do not know how to select a message.
I was thinking about using Select in a Select, but I don't know how to implement it, or if this is the right way to do it.

SELECT DISTINCT person_send_id, person_receive_id 
FROM `message` 
WHERE person_send_id = 21 OR person_receive_id = 21 
//AND SELECT message FROM `message` when these criteria are met

Message table

message_id 
message 
person_send_id 
person_receive_id

Here are my table data.

enter image description here

I want to select only the first message in which person_send_id and person_receive_id are repeated.
Therefore, if I send 10 messages to the same person, I only want to capture only the record with the message, in this case "Hello", for example

+4
1

message .

SELECT DISTINCT person_send_id, person_receive_id, message 
FROM `message` 
WHERE person_send_id = 21 OR person_receive_id = 21;

, , .

ORDER BY message_id DESC LIMIT 1

.

 SELECT DISTINCT person_send_id, person_receive_id, message 
 FROM `message` 
 WHERE person_send_id = 21 OR person_receive_id = 21 
 ORDER BY message_id DESC LIMIT 1;
+5

Source: https://habr.com/ru/post/1677098/


All Articles