First, the scene features
Demand show :
查询出好友给我发的最近一条信息
Second, the database settings
id
: Primary key idsend_user_id
: The sender idreceive_user_id
: Recipient idcontent
:Send Contentsend_time
: Send Time
Third, code implementation
Requirements: query user id for all the information and the latest chat 1
First, not all data before query:
two, sql:
SELECT
*
FROM
(
SELECT
*
FROM
fa_hx_chat_content
WHERE
send_time IN (
SELECT
MAX( send_time )
FROM
fa_hx_chat_content
WHERE
receive_user_id = 1
OR send_user_id = 1
GROUP BY
CONCAT(
IF
( send_user_id > receive_user_id, send_user_id, receive_user_id ),
IF
( send_user_id < receive_user_id, send_user_id, receive_user_id )))
AND ( receive_user_id = 1 OR send_user_id = 1 )
ORDER BY
id DESC
) c
GROUP BY
c.send_time ASC
Third, after the query data: