sql管理两张表 分组并取 分组中的最大值的数据

SELECT
    b.*,c.*
FROM
    (
        SELECT
            MAX(visitDate) as date,
            cgid
        FROM
            crm_guest_visit
        GROUP BY
            cgid
    ) AS a,
    crm_guest_visit AS b,
    crm_guest AS c
WHERE
    a.cgid = b.cgid
AND a.date = b.visitDate
AND a.cgid = c.id -- and c.`name`='马云'

 

 

posted @ 2020-09-03 15:56  三水木00  阅读(705)  评论(0编辑  收藏  举报