mysql 自我连接表

SELECT
    count(*) AS count 
FROM
    aliexpress_online_listing_update AS t1
    INNER JOIN (
    SELECT
        t2.id,
        t2.listing_online_id,
        t2.item_id,
        t2.sku,
        t2.shop_id,
        t2.type,
        MAX( t2.created_at ) AS maxdate 
    FROM
        aliexpress_online_listing_update AS t2 
    WHERE
        t2.push_status = 0 
    GROUP BY
        t2.listing_online_id,
        t2.item_id,
        t2.sku,
        t2.shop_id,
        t2.type 
    ) AS t3 ON t1.listing_online_id = t3.listing_online_id 
    AND t1.item_id = t3.item_id 
    AND t1.sku = t3.sku 
    AND t1.shop_id = t3.shop_id 
    AND t1.type = t3.type 
    AND t1.created_at = t3.maxdate;

表   aliexpress_online_listing_update 先查询最大创建时间的表

SELECT
        t2.id,
        t2.listing_online_id,
        t2.item_id,
        t2.sku,
        t2.shop_id,
        t2.type,
        MAX( t2.created_at ) AS maxdate 
    FROM
        aliexpress_online_listing_update AS t2 
    WHERE
        t2.push_status = 0 
    GROUP BY
        t2.listing_online_id,
        t2.item_id,
        t2.sku,
        t2.shop_id,
        t2.type 
然后与原来的表进行二次连接
 
posted on 2021-05-13 11:55  kevin_yang123  阅读(155)  评论(0编辑  收藏  举报