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

48次阅读

共计 344 个字符,预计需要花费 1 分钟才能阅读完成。

        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

正文完
 0