mysql subquery inside a LEFT JOIN

前端 未结 2 1838
遇见更好的自我
遇见更好的自我 2021-02-12 21:44

I have a query that needs the most recent record from a secondary table called tbl_emails_sent.

That table holds all the emails sent to clients. And most c

2条回答
  •  旧时难觅i
    2021-02-12 22:09

    It should be like this, you need to have a separate query to get the maximum date (or the latest date) that the email was sent.

    SELECT  a.*, b.*
    FROM    tbl_customers a
                INNER JOIN tbl_emails_sent b
                    ON a.customerid = b.customerid
                INNER JOIN
                (
                    SELECT      customerid, MAX(datesent) maxSent
                    FROM        tbl_emails_sent
                    GROUP BY    customerid
                ) c ON  c.customerid = b.customerid AND
                        c.maxSent = b.datesent
    

提交回复
热议问题