Untitled

私信一个人可以称作对一个人进行私信:

数据库这样设计成这样:

SQL语句

查询查询一个人所有会话的最后一条数据:

select <include refid="selectFields"></include>
from message
where id in (
    select max(id) from message
    where status != 2
    and from_id != 1
    and (from_id = #{userId} or to_id = #{userId})
    group by conversation_id
)
order by id desc
limit #{offset}, #{limit}

查询两个会话的全部数据:

select <include refid="selectFields"></include>
from message
where status != 2
and from_id != 1
and conversation_id = #{conversationId}
order by id desc
limit #{offset}, #{limit}

查询未读消息数量:

select count(id)
from message
where status = 0
and from_id != 1
and to_id = #{userId}
<if test="conversationId!=null">
    and conversation_id = #{conversationId}
</if>

更新一批id设置成已读:

update message set status = #{status}
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
    #{id}
</foreach>

插入一条私信:

insert into message(<include refid="insertFields"></include>)
  values(#{fromId},#{toId},#{conversationId},#{content},#{status},#{createTime})