MySQL JOIN the most recent row only?

后端 未结 8 1363
傲寒
傲寒 2020-11-28 03:06

I have a table customer that stores a customer_id, email and reference. There is an additional table customer_data that stores a historical record of the changes made to the

8条回答
  •  春和景丽
    2020-11-28 03:11

    If you are working with heavy queries, you better move the request for the latest row in the where clause. It is a lot faster and looks cleaner.

    SELECT c.*,
    FROM client AS c
    LEFT JOIN client_calling_history AS cch ON cch.client_id = c.client_id
    WHERE
       cch.cchid = (
          SELECT MAX(cchid)
          FROM client_calling_history
          WHERE client_id = c.client_id AND cal_event_id = c.cal_event_id
       )
    

提交回复
热议问题