乐趣区

关于mysql:101mysql-left-join-一对多取右表最新一条记录

        SELECT
            a.*,
            e.userName as chatUser,
            e.content as content,
            e.chatTime as chatTime
        FROM
            gp_group AS a
        LEFT JOIN (
            SELECT b.* FROM chat_content b
            LEFT JOIN (SELECT MAX(c.id) AS id, c.groupUnid FROM chat_content c GROUP BY c.groupUnid
            ) AS d ON d.groupUnid = b.groupUnid
        WHERE b.id = d.id
        ) AS e ON a.id = e.groupUnid
        where 1=1
        <if test="qo.status != null" >
            and a.status = #{qo.status}
        </if>
        order by e.chatTime desc
退出移动版