在where条件后面进行筛选使用exists和having结合的高级用法

SELECT
        wl.leave_id,
        wl.created_time,
        wl.leave_user_id,
        wl.leave_type,
        DATE_FORMAT(wl.start_date,'%Y-%m-%d') start_date,
        FORMAT(wl.leave_duration,2) leave_duration,
        wl.start_time,
        wl.end_time,
        wl.leave_note,
        bwf.audit_id AS auditUserId,
        bwf.audit_status,
        bwf.audit_time
        FROM
            work_leave wl
            JOIN base_work_flow bwf ON wl.leave_id = bwf.data_id
        where
            wl.tenant_id = #{param.tenantId}
            and  EXISTS (
            SELECT
            MAX( bwf2.updated_time )
            FROM
            base_work_flow bwf2
            WHERE
            bwf.data_id = bwf2.data_id
            GROUP BY
            bwf2.data_id
            HAVING
            MAX( bwf2.updated_time ) = bwf.updated_time
            )

 

你可能感兴趣的:(mysql)