Chat mysql database query table settings and chats

First, the scene features

Demand show :查询出好友给我发的最近一条信息
Here Insert Picture Description

Second, the database settings

Here Insert Picture Description

  • id: Primary key id
  • send_user_id: The sender id
  • receive_user_id: Recipient id
  • content:Send Content
  • send_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:
Here Insert Picture Description
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:
Here Insert Picture Description

Published 328 original articles · won praise 110 · views 470 000 +

Guess you like

Origin blog.csdn.net/qq_42249896/article/details/104033697