我有一个名为message的表。我只是在person_send_id和person_receive_id不同时才尝试从表中选择消息。
我可以正确选择person_send_id和person_receive_id。
但是,我不知道如何选择信息。我正在考虑在Select中使用Select,但我不知道如何实现它,或者它是否正确的方法。
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_id
message
person_send_id
person_receive_id
这是我的表数据。
我想只选择person_send_id和person_receive_id重复的第一条消息。
因此,如果我向同一个人发送10条消息,我只想抓取该消息的记录,在这种情况下" Hello"例如
答案 0 :(得分:5)
您只需将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
e.g。
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;