Ronnel.D Ronnel.D - 1 month ago 10
SQL Question

How can I have a SQL Max on a LEFT JOIN?

How can i find the MAX value of tenant_id on a LEFT JOIN of the given stall_id?

$sql = "SELECT * FROM stall s
LEFT JOIN tenant t
ON t.stall_id = s.stall_id
LEFT JOIN rent r
ON r.tenant_id = t.tenant_id AND r.rent_status = 1
WHERE s.stall_id = 1";

Answer

Do you want like this one:-

    SELECT * FROM stall s 
    LEFT JOIN tenant t 
    ON t.stall_id = s.stall_id
    LEFT JOIN rent r
    ON r.tenant_id = t.tenant_id AND r.rent_status = 1
    WHERE s.stall_id = 1 ORDER BY r.tenant_id desc LIMIT 1