首页 > 代码库 > 稍微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出

稍微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出

项目中有一个业务需求是:默认加载15条历史记录(按时间顺序从早到晚)。

下面是我构造的sql逻辑,mark一下,亲测可行。

SELECT * FROM (SELECT *FROM group_chatmsg_v
		WHERE ((group_Id=46 AND send_user_id=28 AND receive_user_id=70)
		OR (group_Id=46 AND receive_user_id=28 AND STATUS=1))
		AND is_delete =0
		ORDER BY crtime DESC LIMIT 15) 
		AS chatMsgTable
		ORDER BY crtime ASC