Alternative to subqueries
        Posted  
        
            by 
                Juanma
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Juanma
        
        
        
        Published on 2012-06-25T14:42:15Z
        Indexed on 
            2012/06/25
            15:16 UTC
        
        
        Read the original article
        Hit count: 240
        
I'm using Mysql 5.1, and have this query, is there a way to not use the subqueries and accomplish the same result?
SELECT oref.affiliate_id, ROUND(sum( oph.amount ) * 0.10 ,2) AS tsum
FROM operators_referer AS oref 
LEFT JOIN operators_payments_history AS oph 
ON oref.operator_id = oph.operator_id
WHERE oref.affiliate_id = 28221
AND
(
    oph.date_paid > 
    (
        SELECT MAX(aph.date_paid) 
        FROM affiliates_payments_history AS aph 
        WHERE aph.operator_id = oref.affiliate_id
    )
    OR 
    (
        SELECT MAX(aph.date_paid)
        FROM affiliates_payments_history AS aph 
        WHERE aph.operator_id = oref.affiliate_id 
    ) 
    is NULL
)
© Stack Overflow or respective owner