SELECT
l.leave_id,
lt.transection_id,
l.leave_emp_id,
lt.active_transection
FROM
leave AS l
INNER JOIN leave_transection AS lt ON l.leave_id = lt.leave_id
WHERE lt.active_transection NOT IN ('NoApprove','Approve');
SELECT
l.leave_id,
lt.transection_id,
l.leave_emp_id,
lt.active_transection
FROM
leave AS l
INNER JOIN leave_transection AS lt ON l.leave_id = lt.leave_id
WHERE lt.active_transection NOT IN ('NoApprove','Approve')
AND l.leave_id IN (SELECT MAX(leave_id) FROM table....)
วิธีแบบถึก ๆ ครับ เอา MAX ของ Leave ID ที่มีตามเงื่อนไขที่กำหนด
SELECT
l.leave_id,
lt.transection_id,
l.leave_emp_id,
lt.active_transection
FROM
leave AS l
INNER JOIN leave_transection AS lt ON l.leave_id = lt.leave_id
INNER JOIN (
SELECT l.leave_id,max(lt.transection_id) as max_tran_id
FROM leave AS l
INNER JOIN leave_transection AS lt ON l.leave_id = lt.leave_id
WHERE lt.active_transection NOT IN ('NoApprove','Approve');
group by l.leav_emp_id ) AS rr ON l.leave_id = rr.leave_id and lt.transection_id = rr.max_tran_id